NETWORK_ERROR:XMLHttpRequest 异常 101

2022-01-15 00:00:00 xmlhttprequest ajax

我收到此错误

NETWORK_ERROR:XMLHttpRequest 异常 101

NETWORK_ERROR: XMLHttpRequest Exception 101

当试图从一个站点获取 XML 内容时.

when trying to get XML content from one site.

这是我的代码:

    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);

有人有解决办法吗?

推荐答案

如果您提供的url位于您的服务器外部,并且服务器不允许您发送请求,则您有权限问题.您不能使用 XMLHttpRequest 从另一台服务器访问数据,除非服务器明确允许您这样做.

If the url you 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.

更新: 意识到这现在可以在 Google 上看到作为答案,我试图找到有关此错误的一些文档.这太难了.

Update: Realizing this is now visible as an answer on Google, I tried to find some documentation on this error. That was surprisingly hard.

这篇文章,有一些背景信息和解决步骤.具体来说,它在这里提到了这个错误:

This article though, has some background info and steps to resolve. Specifically, it mentions this error here:

只要将服务器配置为允许来自您的 Web 应用程序的请求,XMLHttpRequest 就可以工作.否则会抛出 INVALID_ACCESS_ERR 异常

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

INVALID_ACCESS_ERR 的解释似乎是我们在这里看到的.

An interpretation of INVALID_ACCESS_ERR seems to be what we're looking at here.

要解决这个问题,必须将接收请求的服务器配置为允许来源.这在 Mozilla 的更多详细信息中有描述.

To solve this, the server that receives the request, must be configured to allow the origin. This is described in more details at Mozilla.

相关文章