[英]JQuery Ajax Error {“readyState”:0,“responseText”:“”,“status”:0,“statusText”:“OK”}
[英]jquery ajax readystate 0 responsetext status 0 statustext error
我收到以下错误: jquery ajax readystate 0 responsetext status 0 statustext error
给予它: url(http://www.tutorialspoint.com/prototype/prototype_ajax_response.htm)
,但是当我给它url(localhost:""/embparse_page)
时它工作正常url(localhost:""/embparse_page)
我的localhost上的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.
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);" />
使用Javascript
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无法从site2窃取内容并假装它是site1的内容。 这方面的方法是JSONP(google maps使用我认为)并且让site2
提供cors头但jQuery 1中不支持cors *(也许不在2. *中)因为IE在实现它时遇到了一些问题。 在这两种情况下,您都需要site2与您的网站合作,以便您的网站可以显示其内容。
如果你自己只使用它,那么你可以使用Firefox并安装forcecors插件。 要激活,您可以选择view => toolbars => add on bar并单击屏幕右下角的“cors”文本。
我从Ajax调用中得到了这个错误,为我修复它的原因只是放入'return false'。
我和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"); });
我正在测试为json/xml
数据读取txt/XML
文件并得到错误......读取的值: Status[0] & readyState[0]
和StatusText[error];
这在Internet Explorer上成功运行,但在Chrome上无法运行,因为域名必须相同
这就是修复它的原因
转到C:\\ WINDOWS \\ system32 \\ drivers \\ etc \\ hosts
为您的localhost应用程序命名:
127.0.0.1 SampleSiteName.com
现在以http://SampleSiteName.com/YourAjaxFileName.htm
打开chrome中的代码(如果它打开,则表示您已正确输入主机名)转到HTML
文件并提供您所在文件的相对地址想读(如果FileToBeRead.txt
是在同一文件夹中YourAjaxFileName.htm
,那么只需输入网址:“ /FileToBeRead.txt
”)
现在,您的代码也适用于Chrome。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.