Internet Explorer 8中出现Javascript/Ajax错误消息!顺便说一下,这还不是最令人震惊的部分

there's a Javascript/Ajax error message in Internet Explorer 8! by the way this is not the shocking part

本文关键字:一下 Javascript Explorer Ajax 错误 消息 Internet      更新时间:2023-09-26
function a (){
    var b = null;
    if(window.XMLHttpRequest) b = new XMLHttpRequest();
    else if(window.ActiveXObject) b = new ActiveXObject("Microsoft.XMLHTTP");
    if(!b) b = new ActiveXObject("msxml2.XMLHTTP");
    return b;
}
var b=a();
function c(){
    var u = "ajax3_php.php?w="+ parseInt(Math.random()*88888);
    b.onreadystatechange = function (){
        if(b.status == 200 && b.readyState == 4){
            document.getElementById("divid").innerHTML = b.responseText;
        }
    }
    b.open("get", u, true);
    b.send();
}

在目标页面"ajax3_php.php":

<?php
echo "OK";
?>

和"divid" id
& lt;div id = "divid "> Test

错误信息是:

Message: Unspecified error.
Line: 20
Char: 3
Code: 0
URI: http://localhost/lat1/ajax/ajax3.php

令人震惊的部分是,这是在第20行:

if(b.status == 200 && b.readyState == 4){


这有什么不对?
供参考:firebug什么也没说

这段代码似乎奏效了。

function a()
{
    var b = null;
    if(window.XMLHttpRequest)
    {
        b = new XMLHttpRequest();
    }else if(window.ActiveXObject)
    {
        b = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(!b)
    {
        b = new ActiveXObject("msxml2.XMLHTTP");
    }
    return b;
}
var b = a();
function c()
{
    var u = "ajax3_php.php?w="+ parseInt(Math.random()*88888, 10);
    b.open("get", u, true);
    b.onreadystatechange = function ()
    {
        if(b.readyState === 4)
        {
            if(b.status === 200)
            {
                if(b.responseText)
                {
                    document.getElementById("divid").innerHTML = b.responseText;
                }
            }   
        }
    };
    b.send();
}
c();

我没有一个银弹的答案,为什么错误弹出,但调用b.status抛出错误时,readyState不是4。然而,这个特定的错误是我最近遇到的(但不同的错误类型),当通过innerHTML发送一个空字符串到IE 8中的一个元素(我怀疑你的responseText是空的,导致错误)。

我冒昧地为您修复了该代码(以及其他一些触摸)。在IE 7-8中没有错误,怪癖或没有。

我希望这对你有帮助。

马特编辑:我偶然发现了这篇MSDN帖子。奇怪的是,他们的示例代码像我一样使用嵌套的if块,但没有解释。我将进一步调查。

链接:http://msdn.microsoft.com/en-us/library/dd576252%28v=vs.85%29.aspx