我收到以下错误:jquery ajax readystate 0 responsetext status 0 statustext error
时给出它:url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)
,但是当我在本地主机上url(localhost:""/embparse_page)
它时它工作正常。
我尝试使用我在 Google 搜索中找到的标题,我也使用过beforeSend:""
,但它仍然不起作用。
我认为主要问题是:XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.
但我不明白。
谁能向我解释这个问题,因为我对此很陌生。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:ng="http://angularjs.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta Access-Control-Allow-Origin="*" />
<title>Page Parsing</title>
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script>
getit=function(){
jQuery.support.cors = true;
$.ajax({
type:"GET",
url:"http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm",
dataType:"html",
crossDomain:true,
beforeSend: function(xhr) {
xhr.overrideMimeType('text/plain;charset=UTF-8');
},
success:function(XMLHttpRequest,jqXHR ,data) {
//alert(data.title);
var starttitl=data.lastIndexOf('<title>');
var endtitl=data.lastIndexOf('</title>');
var title1=data.substring(starttitl+7,endtitl);
alert(title1);
},
error:function(errorStatus,xhr) {
alert("Error"+JSON.stringify(errorStatus));
}
});
}
</script>
</head>
<body>
<div id="siteloader">
<input type="button" onclick="getit()" />
</div>
</body>
</html>
我收到此错误,就我而言,这不是由于同源策略。我从这个链接得到了一些帮助。其他可能的原因可以在这里看到。
我的情况是,我有一个链接按钮,我没有使用 e.PreventDefault()
.ASPX
<asp:LinkButton ID="lnkSearch" runat="server" CssClass="DockCmdSearch" CommandName="Search" OnClientClick="return VerifySearch(this, event);" />
爪哇语
function VerifySearch(sender, e) {
e.preventDefault();
$.ajax({
type: 'POST',
.............
}
return false;
}
同源策略。 浏览器不允许 当您打开
http://site1.com
要连接到:
site2.com
sub.site1.com
site1:99.com
https://site1.com (not sure about this one)
这样 site1 就无法从站点 2 窃取内容并假装它是站点 1 的内容。解决这个问题的方法是JSONP(我认为谷歌地图使用),并且site2
提供cors标头,但在jQuery 1.*中不支持cors(也可能在2.*中也不支持),因为IE在实现它时遇到了一些问题。在这两种情况下,您都需要 site2 与您的网站合作,以便您的网站可以显示其内容。
如果您只自己使用它,那么您可以使用 Firefox 并安装 forcecors 插件。要激活,您可以选择视图 => 工具栏 => 添加栏,然后单击屏幕右下角的文本"cors"。
我从我的 Ajax 调用中收到了该错误,为我修复它的只是输入"返回错误"。
我在Nginx(服务器端)和AngularJs(用户端)上遇到了同样的问题
正如其他开发人员所说,它的Cors问题,在这里我只想说我如何解决我的问题,也许有人使用这种方法;)
首先,我将以下行添加到我的 Nginx 配置文件(在 linux 中 ->/etc/nginx/sites-available/your domain):
add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
使用 angularJs 我像这样发送我的请求:
$http({
method: "POST",
data: { //params},
url: "//address",
headers: { //if its form data
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"
},
crossDomain: true,
contentType: false,
processData: false
}).then(function mySucces(response) {
alert(JSON.stringify(response));
}, function myError(response) {
alert("error");
});
我正在测试读取txt/XML
文件以获取json/xml
数据并遇到错误...值为:Status[0] & readyState[0]
和StatusText[error];
这在Internet Explorer上成功运行,但在Chrome上没有成功,因为域需要相同
这就是修复它的原因
转到 C:\WINDOWS\system32\drivers\etc\hosts
为您的本地主机应用程序命名:
127.0.0.1 SampleSiteName.com
现在在 chrome 中按http://SampleSiteName.com/YourAjaxFileName.htm
打开代码(如果打开,则表示您输入了正确的主机名)转到您的HTML
文件并给出您尝试读取的文件的相对地址(如果FileToBeRead.txt
与YourAjaxFileName.htm
位于同一文件夹中,则只需输入 url:"/FileToBeRead.txt
")
现在,您的代码也可以在Chrome上运行。