Javascript IE上的Ajax请求错误
我在IE浏览器上有一个小问题(实际上在谷歌Chrome上也有) 我有这个js代码Javascript IE上的Ajax请求错误,javascript,ajax,Javascript,Ajax,我在IE浏览器上有一个小问题(实际上在谷歌Chrome上也有) 我有这个js代码 function createDoc(url) { var xhttp = ajaxRequest(); var currentLocationBase = window.location.href; currentLocationBase = currentLocationBase.substr(0,currentLocationBase.lastIndexOf("/") + 1);
function createDoc(url) {
var xhttp = ajaxRequest();
var currentLocationBase = window.location.href;
currentLocationBase = currentLocationBase.substr(0,currentLocationBase.lastIndexOf("/") + 1);
var u = currentLocationBase + url;
xhttp.open("GET", u, false);
xhttp.send(null);
var xml = xhttp.responseXML;
return xml;
}
/**
* Builds an AJAX reques handler.
*
* @return The handler.
*/
function ajaxRequest() {
var xhttp = null;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
// Internet Explorer 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
}
return xhttp;
}
在Firefox中,这段代码非常有效,但在IE和Google Chrome中则不然
似乎错误是在第行给出的
xhttp.open("GET", u, false);
有人能帮我理解我做错了什么吗?
谢谢因为Ajax是异步的,所以您需要在onreadystatechange代码中处理代码和响应。试一试 看起来您正在发送请求,在读取responseXML之后,这一定会导致问题
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
为什么不部署jQuery?带有优化功能,无需进行特定于浏览器的嗅探。相对于库的包含,您确实会获得更多的应用程序权重,但这绝对是值得的。我不想添加任何框架库。这是一个只需要由我创建的应用程序。