重载/刷新后ajax xhr.status = 0错误,如何防止呢?

时间:2011-10-11 01:05:55

标签: ajax firefox google-chrome xmlhttprequest firefox3.6

我在页面上运行多个ajax请求,我所知道的代码没有任何问题。只是当ajax调用直到运行并且用户选择重新加载或刷新时,浏览器将在ff3和chrome 14中返回状态错误0(IE6 +没有此问题)。

由于我有很多请求,浏览器会多次提醒并且非常烦人。我已经读过这是一个mozilla / firefox 3的bug,它可能已经在更高版本的ff中得到修复,但是chrome 14显然仍然有这个错误。但我正在构建ff3和chrome兼容性。有一个错误报告Here

有人知道如何仅为重新加载/刷新抑制状态错误吗?

TIA

我的ajax代码

function createXHR() {
    var xhrObj;
    if (window.XMLHttpRequest) {
        // branch for native XMLHttpRequest object - Mozilla, IE7
        try {
            xhrObj = new XMLHttpRequest();
        } 
        catch (e) {
            alert("Your browser does not support this website!");
            xhrObj = null;
        }//close catche
    }  //close if
    else if (window.ActiveXObject) {
        // branch for IE/Windows ActiveX version
        try {
            xhrObj = new ActiveXObject("Msxml2.XMLHTTP");
        } //try
        catch(e) {
            try{
                xhrObj = new ActiveXObject("Microsoft.XMLHTTP");
            }//inner try
            catch(e) {
                alert("Your browser does not support this website!");
                xhrObj = null;
            } //inner catch
        }//catch
    } //else if

    return xhrObj;
}//eof createXHR

function search(var1, var2) {
    var xhrObj = createXHR();
    if (xhrObj) {
        try {
    var queryString = "whatever";   
        xhrObj.open("GET", "url.php"+queryString,true);             
        xhrObj.onreadystatechange = function (){callback(xhrObj,var1,var2);};
        xhrObj.send(null);
        } catch (e) {
            alert ('search error');
            return;
        }
    } else {
       alert("search error has occured, please refresh and try again");
       return; //do plan B. You do have a plan B, no?
    }
}//tld close

function callback(xhr,var1, var2) { 
    if (xhr.readyState == 4) {
        try { 
            if (xhr.status == 200) 
                {
        var s = xhr.responseText;           
                //dosomething
                }
            else {
                alert('Status error '+xhr.status);
        return; 
        }
        } 
        catch (e) {
            alert("live Server Error "); 
        return;
        //this will alert if a call is issued to a nonexistent function / variable
        }
    }
}//callback close

0 个答案:

没有答案