【发布时间】:2011-06-24 18:20:10
【问题描述】:
当尝试执行 XMLHttpRequest 时,响应从服务器返回(在 Fiddler 中检查),但 xhr.getAllResponseHeaders() 返回 null 并引发异常。
是因为“同源政策”吗?你能建议如何解决这个问题吗?
代码:使用datajs.codeplex.com开源代码:
xhr.onreadystatechange = function () {
if (xhr === null || xhr.readyState !== 4) {
return;
}
// Workaround for XHR behavior on IE.
var statusText = xhr.statusText;
var statusCode = xhr.status;
if (statusCode === 1223) {
statusCode = 204;
statusText = "No Content";
}
var headers = [];
var responseHeaders = xhr.getAllResponseHeaders().split(/\r?\n/);
资源位于不同的域中。访问http://odata.netflix.com/v1/Catalog/Genres
【问题讨论】:
-
一些代码示例将有助于回答可能出现的问题。
-
请发布代码示例。听起来好像您在返回之前检查了响应。
-
同源策略仅适用于您尝试访问不位于您的域中的资源。由于您没有指定要访问的内容,因此答案是:也许。
-
@geoffreyd @leeeb 代码:使用 datajs.codeplex.com 开源代码。 xhr.onreadystatechange = function () { if (xhr === null || xhr.readyState !== 4) { return; } 变量标题 = []; var responseHeaders = xhr.getAllResponseHeaders().split(/\r?\n/);抛出异常 xhr.getAllResponseHeaders() 为 null
-
同源策略阻止跨域 XHR。另外,请记住,如果状态码是 1223/204(即使是同源),getAllResponseHeaders 调用将返回 null。
标签: javascript xmlhttprequest same-origin-policy