問題描述
無法通過 XMLHttpRequest 獲取數據(狀態 0 且 responseText 為空):
<上一頁>xmlhttp=新的 XMLHttpRequest();xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true);xmlhttp.onreadystatechange=函數(){如果(xmlhttp.readyState==4)警報(狀態" + xmlhttp.status);}xmlhttp.send();它會提醒狀態 0".
與localhost請求相同的情況(cd_catalog.xml保存為本地文件)
<上一頁>xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);但是使用 localhost IP 請求
<上一頁>xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);并與本地文件請求
<上一頁>xmlhttp.open("GET","cd_catalog.xml", true);一切正常(狀態 200)
在線請求出現問題 (status=0) 的原因是什么?
PS:Live HTTP Headers 顯示在所有 4 種情況下一切正常:
<上一頁>HTTP/1.1 200 正常內容長度:4742PS2:VMWare 上的 Apache 本地 Web 服務器(主機操作系統 Win7、來賓操作系統 Ubuntu、網絡適配器 – NAT).瀏覽器 – 火狐.
當你的包含腳本的 html 文件通過文件方案在瀏覽器中打開時,狀態為 0.確保將文件放在您的服務器(apache 或 tomcat 等)中,然后在瀏覽器中通過 http 協議打開它.(即 http://localhost/myfile.html)這就是解決方案.
Cannot get data with XMLHttpRequest (status 0 and responseText is empty):
xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true); xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) alert("status " + xmlhttp.status); } xmlhttp.send();
It alerts "status 0".
The same situation with the localhost request (cd_catalog.xml is saved as a local file)
xmlhttp.open("GET","http://localhost/cd_catalog.xml", true);
But with the localhost IP request
xmlhttp.open("GET","http://127.0.0.1/cd_catalog.xml", true);
and with the local file request
xmlhttp.open("GET","cd_catalog.xml", true);
everything is OK (status 200)
What can cause the problem (status=0) with the online request?
PS: Live HTTP Headers shows that everything is OK in all 4 cases:
HTTP/1.1 200 OK Content-Length: 4742
PS2: Apache local web server on VMWare (host OS Win7, Guest OS Ubuntu, Network adapter – NAT). Browser – Firefox.
status is 0 when your html file containing the script is opened in the browser via the file scheme. Make sure to place the files in your server (apache or tomcat whatever) and then open it via http protocol in the browser. (i.e. http://localhost/myfile.html) This is the solution.
這篇關于XMLHttpRequest 狀態 0(responseText 為空)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!