是来自浏览器或 XmlHttpRequest 的“没有'访问控制-允许源'标头”错误消息

Is the "No 'Access-Control-Allow-Origin' header" error message from the browser or from XmlHttpRequest?

本文关键字:许源 标头 消息 错误 访问控制 XmlHttpRequest 没有 浏览器      更新时间:2023-09-26

使用以下代码执行(故意失败的(跨域请求时:

var request = new XMLHttpRequest();
request.open("GET", 'http://code.jquery.com/jquery-2.1.1.min.js');
request.onerror = function(error) {
    alert(error.target.status)
};
request.send()

我将在开发人员控制台中收到以下预期的错误消息:

XMLHttpRequest cannot load http://code.jquery.com/jquery-2.1.1.min.js No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

但是,此错误消息是来自 XmlHttpRequest 还是来自浏览器?

我想从 XmlHttpRequest 中获取该错误消息,但我看不到在哪里。

使用上面的代码,我希望(如 https://stackoverflow.com/a/16931075/343475 所示(在警报框中看到错误消息,但我得到的只是0.

但是,此错误消息是来自 XmlHttpRequest 还是来自浏览器?

XMLHttpRequest 是浏览器的一部分。所以"是"。

如果您的意思是"错误来自服务器还是来自浏览器",那么它来自浏览器。服务器无法知道您是否正在发出跨源请求。

使用上面的代码,我希望(如 https://stackoverflow.com/a/16931075/343475 所示(在警报框中看到错误消息,但我得到的只是 0。

状态代码将提供有关外部源上的资源的信息(它会告诉您资源是否存在以及浏览器的用户是否有权访问它(,因此它被同源策略禁止。

相关文章: