How to handle XHR error display messages?

对着背影说爱祢 提交于 2020-01-02 05:48:05

问题


I'm trying to fetch some content from another source using XHR as shown below:

function fetchPage(str)
{
    if(str=="")
    {
        document.getElementById("table").innerHTML="";
        resetFilters();
        $('#progress').hide();  //fetching progress bar <div>
        return;
    }
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    else // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=postCallback;
    xmlhttp.open("GET", "fetch.php?url=http://www.sis.itu.edu.tr/tr/ders_programlari/LSprogramlar/prg.php?fb="+str, true);
    xmlhttp.send();

// any stuff that goes here will happen before callback
//  (this is a good place to update a UI element showing a call is resolving.)
//  (for example a spinner or text saying "fetching")
$('#progress').show();
progressFetching();
switch(xmlhttp.readyState){ //loading bar adjustments
    case 0:
        $('.bar').css("width","0%");
        $('.bar').text("0%");
        break;
    case 1:
        $('.bar').css("width","25%");
        $('.bar').text("25%");
        break;
    case 2:
        $('.bar').css("width","50%");
        $('.bar').text("50%");
        break;
    case 3:
        $('.bar').css("width","75%");
        $('.bar').text("75%");
        break;
}
}



function postCallback()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
    progressDone();   //loading is finished
    $('#error').hide();
    document.getElementById("table").innerHTML=xmlhttp.responseText;                     

    // continue to process post callback.
    resetFilters();
}
else {
    // report error with fetch

    /*if(xmlhttp.status==404 || xmlhttp.responseText == "")
        $('#error').show();*/
    //$('#error').show();
}
}

I want my page to display error when connection timeout occurs, or when the computer doesn't have an internet connection (maybe a disconnection occurred while hanging around) or any other situation where the webpage fails to fetch the contents of the other source.

Using the code above, in the else block, if I go for if(xmlhttp.status==404 || xmlhttp.responseText == "") in the /* */ comment section, I won't get an error unless its not a 404 error. If i go for // comment section, error will be displayed after the fetching process is started until it is completed, i.e. between xmlhttp.readyState = 0 through xmlhttp.readyState = 4. How can I display connection error messages using these attributes or something else?

Thank your for your attention:)


回答1:


The problem is my template in prior question was flawed. I believe this will work better because it creates a closure to pass the variable you need to work with.

Once again, I did not test this so it might have typos and bugs -- nor did I change anything except how postCallback() is invoked and added a parameter to it.

function fetchPage(str)
{
    if(str=="")
    {
        document.getElementById("table").innerHTML="";
        resetFilters();
        $('#progress').hide();  //fetching progress bar <div>
        return;
    }
    var xmlhttp;

    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    else // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=function () { postCallback(xmlhttp); };
    xmlhttp.open("GET", "fetch.php?url=http://www.sis.itu.edu.tr/tr/ders_programlari/LSprogramlar/prg.php?fb="+str, true);
    xmlhttp.send();

// any stuff that goes here will happen before callback
//  (this is a good place to update a UI element showing a call is resolving.)
//  (for example a spinner or text saying "fetching")
$('#progress').show();
progressFetching();
switch(xmlhttp.readyState){ //loading bar adjustments
    case 0:
        $('.bar').css("width","0%");
        $('.bar').text("0%");
        break;
    case 1:
        $('.bar').css("width","25%");
        $('.bar').text("25%");
        break;
    case 2:
        $('.bar').css("width","50%");
        $('.bar').text("50%");
        break;
    case 3:
        $('.bar').css("width","75%");
        $('.bar').text("75%");
        break;
}
}



function postCallback(xmlhttp)
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
    progressDone();   //loading is finished
    $('#error').hide();
    document.getElementById("table").innerHTML=xmlhttp.responseText;                     

    // continue to process post callback.
    resetFilters();
}
else {
    // report error with fetch

    /*if(xmlhttp.status==404 || xmlhttp.responseText == "")
        $('#error').show();*/
    //$('#error').show();
}
}



回答2:


According to this stackoverflow: XMLHttpRequest (Ajax) Error

 xmlhttp.onreadystatechange = function (oEvent) {
   if (xmlhttp.readyState === 4) {
     if (xmlhttp.status === 200) {
       console.log(xmlhttp.responseText)
     } else {
       console.log("Error", xmlhttp.statusText)
     }
   }
 }


来源:https://stackoverflow.com/questions/14715866/how-to-handle-xhr-error-display-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!