gpt4 book ai didi

javascript - 网页在 Firefox 上运行良好,但在 IE 上运行不佳

转载 作者:行者123 更新时间:2023-11-28 14:00:47 24 4
gpt4 key购买 nike

我有以下代码片段:

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;
}

我从 Jeremy Keith 得到这个不久前,它从未让我失望过。

关于javascript - 网页在 Firefox 上运行良好,但在 IE 上运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com