开发者

XMLHttpRequest response is null in Javascript

when trying to execute XMLHttpRequest, the response is returned from server (as checked in Fiddler), but xhr.getAllResponseHeaders() returns null and throws exception.

Is it because of "Same Origin Policy"? Can you please suggest how to resolve the issue?

Code: Using datajs.codeplex.com open source code:

                xhr.onreadystatechange = function () {
                if (xhr === null || xhr.readyState !== 4) {
                   开发者_如何学C 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/);

Resource is located in different domain. Accessing http://odata.netflix.com/v1/Catalog/Genres


To circumvent the same-origin policy you could use JS to call a PHP script that gets the contents of the external url and echo the results.


If the The same-origin policy is your problem, you could use YQL as a proxy.

EDIT: eg. http://developer.yahoo.com/yql/console/#h=select%20*%20from%20atom%2810%29%20where%20url%3D%27http%3A//odata.netflix.com/v1/Catalog/Genres%27

What's nice is that you can ask to get the result as json and consume it easily from your client script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜