ajax NETWORK_ERROR:XMLHttpRequest 异常 101
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2235929/
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
NETWORK_ERROR: XMLHttpRequest Exception 101
提问by pawan Mangal
I am getting this Error
我收到此错误
NETWORK_ERROR: XMLHttpRequest Exception 101
NETWORK_ERROR:XMLHttpRequest 异常 101
when trying to get XML content from one site.
尝试从一个站点获取 XML 内容时。
Here is my code:
这是我的代码:
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
if (xmlhttp==null) {
alert ("Your browser does not support XMLHTTP!");
return;
}
xmlhttp.onReadyStateChange=function() {
if(xmlhttp.readyState==4) {
var value =xmlhttp.responseXML;
alert(value);
}
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
//alert(xmlhttp.responseXML);
}
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
Does any one have a solution?
有没有人有办法解决吗?
回答by Frederik Wordenskjold
If the urlyou provide is located externally to your server, and the server has not allowed you to send requests, you have permission problems. You cannot access data from another server with a XMLHttpRequest, without the server explicitly allowing you to do so.
如果url您提供的位于服务器外部,并且服务器不允许您发送请求,则您存在权限问题。您不能使用 访问来自另一台服务器的数据XMLHttpRequest,除非服务器明确允许您这样做。
Update:Realizing this is now visible as an answer on Google, I tried to find some documentation on this error. That was surprisingly hard.
更新:意识到这现在可以作为 Google 上的答案可见,我试图找到有关此错误的一些文档。这出奇的难。
This articlethough, has some background info and steps to resolve. Specifically, it mentions this error here:
不过,这篇文章有一些背景信息和解决步骤。具体来说,它在这里提到了这个错误:
As long as the server is configured to allow requests from your web application's origin, XMLHttpRequest will work. Otherwise, an INVALID_ACCESS_ERR exception is thrown
只要服务器配置为允许来自 Web 应用程序源的请求,XMLHttpRequest 就可以工作。否则抛出 INVALID_ACCESS_ERR 异常
An interpretation of INVALID_ACCESS_ERRseems to be what we're looking at here.
INVALID_ACCESS_ERR的解释似乎就是我们在这里看到的。
To solve this, the server that receives the request, must be configured to allow the origin. This is described in more details at Mozilla.
回答by Ian
The restriction that you cannot access data from another server with a XMLHttpRequest can apply even if the url just implies a remote server.
即使 url 只是暗示远程服务器,也可以应用不能使用 XMLHttpRequest 从另一台服务器访问数据的限制。
So: url = "http://www.myserver.com/webpage.html"
所以: url = "http://www.myserver.com/webpage.html"
may fail,
可能会失败,
but: url = "/webpage.html"
但是: url = "/webpage.html"
succeed - even if the request is being made from www.myserver.com
成功 - 即使请求是从 www.myserver.com 发出的
回答by Mart van de Sanden
Something like this happened with me when I returned incorrect XML (I put an attribute in the root node). In case this helps anyone.
当我返回不正确的 XML(我在根节点中放置了一个属性)时,类似的事情发生在我身上。万一这对任何人都有帮助。
回答by Ribo
Request aborted because it was cached or previously requested?It seems the XMLHttpRequest Exception 101 error can be thrown for several reasons. I've found that it occurs when I send an XMLHttpRequest with the same URL more than one time. (Changing the URL by appending a cache defeating nonsense string to the end of the URL allows the request to be repeated. -- I wasn't intending to repeat the request, but events in the program caused it to happen and resulted in this exception).
请求因缓存或先前请求而中止?似乎可以出于多种原因抛出XMLHttpRequest Exception 101错误。我发现当我多次发送具有相同 URL 的 XMLHttpRequest 时会发生这种情况。(通过在 URL 末尾附加一个阻止无意义字符串的缓存来更改 URL 允许重复请求。--我不打算重复请求,但程序中的事件导致它发生并导致此异常)。
Not returning the correct responseText or responseXML in the event of a repeated request is a bug (probably webKit).
在重复请求的情况下不返回正确的 responseText 或 responseXML 是一个错误(可能是 webKit)。
When this exception occurred, I did get an onload event with readyState==4 and the request object state=0 and responseText=="" and responseXML==null. This was a cross domain request, which the server permits.
当这个异常发生时,我确实得到了一个 onload 事件,其 readyState==4 和请求对象 state=0 和 responseText=="" 和 responseXML==null。这是服务器允许的跨域请求。
This was on an Android 2.3.5 system which uses webKit/533.1
这是在使用 webKit/533.1 的 Android 2.3.5 系统上
Anyone have documentation on what the exception is supposed to mean?
任何人都有关于异常应该意味着什么的文档?
回答by Lian
xmlhttp.open("GET",url, true);
set the async part to true
将异步部分设置为 true
回答by Martin
I found a very nice article with 2 diferent solutions.
我找到了一篇非常好的文章,其中包含 2 个不同的解决方案。
- The first one implementing jQuery and JSONP, explaining how simple it is.
- The second approach, it's redirecting trough a PHP call. Very simple and very nice.
- 第一个实现 jQuery 和 JSONP,解释了它是多么简单。
- 第二种方法是通过 PHP 调用重定向。非常简单,非常好。
回答by chriz
Another modern method of solving this problem is Cross Origin Ressource Sharing. HTML5 offers this feature. You can "wrap" your XMLhttp request in this CORS_request and if the target browser supports this feature, you can use it and wont have no problems.
另一种解决这个问题的现代方法是跨源资源共享。HTML5 提供了这个功能。您可以在此 CORS_request 中“包装”您的 XMLhttp 请求,如果目标浏览器支持此功能,您可以使用它并且不会有任何问题。
EDIT: Additionaly i have to add that there are many reasons which can cause this Issue. Not only a Cross Domain Restriction but also simply wrong Settings in your WEB.CONFIG of your Webservice.
编辑:另外我必须补充一点,有很多原因可能导致这个问题。不仅是跨域限制,而且是您的 Web 服务的 WEB.CONFIG 中的错误设置。
Example IIS(.NET):
示例 IIS(.NET):
To enable HTTP access from external sources ( in my case a compiled Phonegap app with CORS request ) you have to add this to your WEB.CONFIG
要启用来自外部来源的 HTTP 访问(在我的例子中是一个带有 CORS 请求的已编译 Phonegap 应用程序),您必须将其添加到您的 WEB.CONFIG
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
Another scenario:
另一个场景:
I got two webservices running... One on Port 80 and one on Port 90. This also gave me an XML HTTP Request Error. I even dont know why :). Nevertheless i think this can help many not well experienced readers.
我有两个网络服务正在运行...一个在端口 80 上,一个在端口 90 上。这也给了我一个 XML HTTP 请求错误。我什至不知道为什么:)。尽管如此,我认为这可以帮助许多没有经验的读者。

