XMLHttpRequest 状态 0(响应文本为空) [英] XMLHttpRequest status 0 (responseText is empty)

查看:33
本文介绍了XMLHttpRequest 状态 0(响应文本为空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使用 XMLHttpRequest 获取数据(状态 0 且 responseText 为空):

<前>xmlhttp=新的 XMLHttpRequest();xmlhttp.open("GET","http://www.w3schools.com/XML/cd_catalog.xml", true);xmlhttp.onreadystatechange=function(){如果(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 正常内容长度:4742

PS2:VMWare 上的 Apache 本地 Web 服务器(主机操作系统 Win7、来宾操作系统 Ubuntu、网络适配器 – NAT).浏览器 - Firefox.

解决方案

当包含脚本的 html 文件通过文件方案在浏览器中打开时,status 为 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(响应文本为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆