問題描述
我用的是firefox 3.6.10,用firebug來調試
Im using firefox 3.6.10, and firebug to debug
所以,這是我的代碼:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url,false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(null);
alert(xmlhttp.responseXML);
responseXML 始終為空,我已經在來自不同域的多個 URL 上嘗試過它.我也嘗試過異步,結果是一樣的.responseText 總是正確返回,沒有問題.
responseXML is always null, and i've tried it on several URLs from different domains. I have also tried it asynchronously, it's the same result. The responseText is always properly returned, no problems with it.
我的目標是獲得 responseXML.documentElement.
感謝您的幫助.
編輯-----------
此 javascript 代碼是從 Greasemonkey 用戶腳本執行的,我將其設置為與請求的 url 相同的來源.我也嘗試從螢火控制臺執行,再次確保原產地政策.兩者都有相同的錯誤.
討厭javascript.
EDIT-----------
This javascript code was executed from a Greasemonkey userscript, i made surte its the same origin as the requested url. Also i tried executing from firebug console, again ensuring the origin policy. Same error on both.
Gotta hate javascript.
推薦答案
我敢打賭你違反了同源政策.
I bet you are violating the same origin policy.
對于 XHR,您必須具有相同的協議、域、端口等.因此,如果您在 localhost:8080/app 上運行應用程序,則不能 ajax 到 www.cnn.com.
For XHRs, you must have the same protocol, domain, port, etc. So if you are running an app on localhost:8080/app, you CANNOT ajax to www.cnn.com.
不同的瀏覽器有不同的處理方式;我看到FF按照你的描述做了,就是請求看起來正常返回但是沒有數據...
Different browsers handle this differently; I have seen FF do what you describe, which is the request appears to return normally but there is no data...
這篇關于responseXML 始終為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!