我有这样的代码:阿贾克斯xmlhttp.onreadystatechange功能
function loadRegions()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("Ready:"+xmlhttp.status);
xmlDoc=xmlhttp.responseXML;
x=xmlDoc.getElementsByTagName("region");
alert(x[0]);
alert(x[1]);
}
}
var ctrcode = frm.elements['countrycode'];
xmlhttp.open("GET","http://mydomain.gr/regionslist.php?countrycode="+ctrcode.value,true);
xmlhttp.send();
}
我有一个select
项目在我的HTML,当有人选择一个项目,然后调用此函数来获得这个国家的地区。我可以从控制台看到请求已完成,但响应从未得到。我的onreadystatechange
函数永远不会被调用。当我删除xmlhttp.status==200
时,函数被调用,但xmlDoc
对象是null
和xmlhttp.status==0
。如果我单独调用它,我使用的URL有效。为什么我的onreadystatechange
函数不起作用,为什么它不返回状态200?
原因: 1)responseXML属性代表当完整的HTTP响应已被接收到的XML响应(当readyState为4),当所述Content-Type头指定MIME(媒体)类型为text/xml,application/xml,或以+ xml结尾。如果Content-Type标头不包含这些媒体类型之一,则responseXML值为null。 2)初始响应后,所有事件监听器都将被清除。 在设置onreadystatechange侦听器之前调用open()。 – 2012-07-11 11:26:52
这里是[第一个](http://www.oxymoronical.com/experiments/apidocs/interface/nsIXMLHttpRequest)和[第二个]的参考文献(http://www.devx.com/webdev/Article/33024/ 1954)理由 – 2012-07-11 11:27:31
看起来你的两条评论更适合你的答案。这将把所有的信息放在一个地方,允许进一步的编辑,并允许更准确的投票和答案接受。其他用户将看到答案中的所有信息,这是投票期间考虑的内容。 – 2012-07-12 02:51:46