【发布时间】:2012-04-02 21:46:51
【问题描述】:
我试图在 javascript 中从 php web 服务获取 http 响应,但在 Firefox 和 chrome 中获取 null。请告诉我我的代码在哪里做错了,
function fetch_details()
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest()
alert("first");
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP")
alert("sec");
}
xhttp.open("GET","url.com",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
alert(xmlDoc.getElementsByTagName("Inbox")[0].childNodes[0].nodeValue);
}
我也尝试过使用 ajax,但没有得到 http 响应这是我的代码,请指导我
var xmlhttp = null;
var url = "url.com";
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
alert(xmlhttp); //make sure that Browser supports overrideMimeType
if ( typeof xmlhttp.overrideMimeType != 'undefined')
{
xmlhttp.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert('Perhaps your browser does not support xmlhttprequests?');
}
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
{
alert(xmlhttp.responseXML);
}
};
}
// 发出实际的请求 xmlhttp.send(null);
我得到 xmlhttp.readyState = 4 xmlhttp.status = 0 xmlhttp.responseText = ""
请告诉我哪里出错了
【问题讨论】:
-
请对您的代码格式进行一些处理。文本字段上方有一个“010101”按钮,用于将代码标记为代码。即使我会为你点击它,这段代码还是太乱了,所以这次我就不打扰了。
-
您是在进行跨域请求吗?这是获取状态码 0 的常见情况。