Search code examples
javascriptjqueryxmlhttprequest

How to handle XHR error display messages?


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:)


Solution

  • 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();
    }
    }