jquery ajax readystate 0 responsetext status 0 statustext 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19395354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery ajax readystate 0 responsetext status 0 statustext error
提问by Dhirender Tyagi
I am getting the following error: jquery ajax readystate 0 responsetext status 0 statustext error
when giving it: url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)
, however it's working fine when I give it url(localhost:""/embparse_page)
on my localhost.
我收到以下错误:jquery ajax readystate 0 responsetext status 0 statustext error
when url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)
give it : ,但是当我url(localhost:""/embparse_page)
在我的本地主机上提供它时它工作正常。
I have tried using the headers which I found on a Google search, and I have used beforeSend:""
too, but it's still not working.
我尝试使用在 Google 搜索中找到的标头,我也使用beforeSend:""
过,但仍然无法正常工作。
I think the main problem is: XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.
but I don't understand it.
我认为主要问题是:XMLHttpRequest cannot load http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm. Origin "local server" is not allowed by Access-Control-Allow-Origin.
但我不明白。
Can anyone please explain the problem to me, as I'm quite new to this.
任何人都可以向我解释这个问题,因为我对此很陌生。
<!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>
回答by user007
I was getting this error and in my case it was not due to same origin policy. This answeris quite helpful.
我收到此错误,就我而言,这不是由于同源策略所致。这个答案很有帮助。
My case was, I had a link button and I was not using e.PreventDefault()
我的情况是,我有一个链接按钮,但我没有使用 e.PreventDefault()
ASPX
ASPX
<asp:LinkButton ID="lnkSearch" runat="server" CssClass="DockCmdSearch" CommandName="Search" OnClientClick="return VerifySearch(this, event);" />
Javascript
Javascript
function VerifySearch(sender, e) {
e.preventDefault();
$.ajax({
type: 'POST',
.............
}
return false;
}
回答by HMR
same origin policy. the browser does not allow when you're on
同源政策。当您打开时浏览器不允许
http://site1.com
to connect to:
连接到:
site2.com
sub.site1.com
site1:99.com
https://site1.com (not sure about this one)
This is so site1 cannot steal content from site2 and pretend it's the content of site1. Ways around this is JSONP (google maps use that I think) and having site2
provide cors headers but cors are not supported in jQuery 1.* (maybe not in 2.* either) because IE has some problems implementing it. In both situations you need site2 to cooperate with your site so your site can display it's content.
这使站点 1 不能从站点 2 窃取内容并假装它是站点 1 的内容。解决这个问题的方法是 JSONP(我认为使用谷歌地图)并site2
提供 cors 标头,但 jQuery 1.* 不支持 cors(可能也不支持 2.*),因为 IE 在实现它时存在一些问题。在这两种情况下,您都需要 site2 与您的站点合作,以便您的站点可以显示其内容。
If you only use this yourself then you can use Firefox and install the forcecors plugin. To activate you can choose view => toolbars => add on bar and click on the text "cors" in the right bottom of the screen.
如果您只自己使用它,那么您可以使用 Firefox 并安装 forcecors 插件。要激活,您可以选择视图 => 工具栏 => 添加栏并单击屏幕右下角的文本“cors”。
回答by BlooSki
I was getting that error from my Ajax call, and what fixed it for me was just putting in the 'return false'.
我从我的 Ajax 调用中得到了那个错误,而为我修复的只是输入了“返回假”。
回答by Mhmd
I had same problem with Nginx ( server side ) and AngularJs ( User side )
as other developer said its Cors problem , here i just want to say how i solve my problem maybe some one use this approach ;)
first I added below lines to my Nginx config files( in linux -> /etc/nginx/sites-available/your domain) :
我在 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';
and with angularJs I send my request like this :
和 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");
});
回答by WebCMSProjects
I was testing reading a txt/XML
file for json/xml
data and got an error... the values read: Status[0] & readyState[0]
and StatusText[error];
This was working successfully on Internet explorer but not on Chrome because the domain needed to be the same
我正在测试读取数据txt/XML
文件json/xml
并出现错误...读取的值:Status[0] & readyState[0]
并且StatusText[error];
这在 Internet Explorer 上成功运行,但在 Chrome 上运行不成功,因为域需要相同
This is what fixed it
这就是修复它的原因
Go to C:\WINDOWS\system32\drivers\etc\hosts
转到 C:\WINDOWS\system32\drivers\etc\hosts
Put a name against your localhost app:
为您的本地主机应用程序命名:
127.0.0.1 SampleSiteName.com
Now open the code in chrome as http://SampleSiteName.com/YourAjaxFileName.htm
(if it opens, it means that you have entered a host name correctly)
go to your HTML
file and give a relative address of the file which you are trying to read (if FileToBeRead.txt
is in the same folder as YourAjaxFileName.htm
, then just enter url: "/FileToBeRead.txt
")
现在在 chrome 中打开代码http://SampleSiteName.com/YourAjaxFileName.htm
(如果它打开,则意味着您已正确输入了主机名)转到您的HTML
文件并提供您尝试读取的文件的相对地址(如果FileToBeRead.txt
与YourAjaxFileName.htm
,然后只需输入网址:“ /FileToBeRead.txt
”)
Now your code will work on Chrome as well.
现在您的代码也可以在 Chrome 上运行了。