2011-02-10 126 views
1

當試圖執行XMLHttpRequest時,響應從服務器返回(如在Fiddler中選中),但xhr.getAllResponseHeaders()返回null並拋出異常。XMLHttpRequest響應在Javascript中爲空

是否因爲「同源政策」?你能建議如何解決這個問題嗎?

代碼:使用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

+0

一些代碼的例子可以幫助回答可能出錯的東西。 – geoffreyd 2011-02-10 10:31:45

回答

0

爲了規避同源策略,您可以使用JS來調用一個PHP腳本,該腳本獲取外部url的內容和結果的echo