Javascript Firefox上的网页运行良好,但IE上没有
我有以下代码片段:Javascript Firefox上的网页运行良好,但IE上没有,javascript,ajax,Javascript,Ajax,我有以下代码片段: self.xmlHttpReq = new XMLHttpRequest(); self.xmlHttpReq.onreadystatechange = function() { if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200) { xmlDoc = self.xmlHttpReq.responseXML; var xmlV
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.onreadystatechange = function()
{
if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200)
{
xmlDoc = self.xmlHttpReq.responseXML;
var xmlVar1 = xmlDoc.getElementsByTagName('var1')[0].childNodes[0].nodeValue;
var xmlVar2 = xmlDoc.getElementsByTagName('var2')[0].childNodes[0].nodeValue;
}
}
在IE中,错误代码表示:
object required, ajax request.js line num, char num
但是,同样的ajax请求在Firefox中也可以正常工作。IE和Firefox对于XMLHttpRequest有不同的对象名称,您必须检查浏览器并基于此声明新对象 试着这样做:
function getXHR() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
}
return xhr;
}
这是我很久以前得到的,从来没有让我失望过。IE和Firefox对于XMLHttpRequest有不同的对象名称,你必须检查你的浏览器并基于此声明新对象 试着这样做:
function getXHR() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
}
return xhr;
}
这是我很久以前得到的,它从未让我失望过。Internet Explorer没有
XMLHttpRequest
对象。相反,它使用ActiveX对象实现相同的功能。因此,您需要更改此行:
self.xmlHttpReq = new XMLHttpRequest();
致:
Internet Explorer没有
XMLHttpRequest
对象。相反,它使用ActiveX对象实现相同的功能。因此,您需要更改此行:
self.xmlHttpReq = new XMLHttpRequest();
致:
您有没有特定的理由不使用像jQuery这样的框架来完成Ajax的繁重工作?您有没有特定的理由不使用像jQuery这样的框架来完成Ajax的繁重工作?