Issues with handling http errors with jQuery AJAX webservice calls
<p>I'm developing an jQuery application in where I've a requirement to capture HTTP errors as and when it occurs. Below is my snippet.</p>

<pre><code>// Function to validate URL

function validateURL(url) 
{
    var pattern = new RegExp();
    pattern.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&amp;\?\/.=]+$");
    if (!pattern.test(url)) 
    {
        return false;
    }
    return true;
}

// Generic error handler for handling the webservice requests.
function initWebService(wstype, wsurl,jsonData)
{
    // If the method parameter is not either "GET" or "POST" display an error message to the developer.

    var msgValidateArgument;
    var wsCallStatus;
    var callbackData;

    if ((arguments[0] != 'GET') &amp;&amp; (arguments[0] != 'POST'))
    {
        //alert("Invalid");
        //alert("You must provide a valid http method in your webservice call.");
        msgValidateArgument = "You must provide a valid http method in your webservice call.";
        return msgValidateArgument;
    }

    // Making sure whether the developer is passing the required number of parameters.
    if(arguments.length &lt; 3)
    {
        //alert("Some required arguments seems to be missing. Please check your webservice invocation.");
        msgValidateArgument = "Some required arguments seems to be missing. Please check your webservice invocation.";
        return msgValidateArgument;
    }

    if (!validateURL(arguments[1]))
    {
        msgValidateArgument = "You must provide a valid URL in your webservice call.";
        return msgValidateArgument;
    }

    if(arguments[2] != ''){
        var response=jQuery.parseJSON(arguments[2]);
            if(typeof response =='object'){
                //It is JSON
                alert(response.toSource());
            }
            else{
            msgValidateArgument = "The JSON data being passed is not in valid JSON format.";
            return msgValidateArgument;
        }
    }

    // Making the AJAX call with the parameters being passed. The error handler handles some of the possble http error codes as of now. 

    $.ajax({
        type: arguments[0],
        url: arguments[1],
        data: arguments[2],
        dataType: 'json',
        async:false,
        statusCode:{
            404: function(){
                alert('Page not found');
            },
            500: function(){
                alert('Page not found');
            },
            504: function(){
                alert('Unknown host');
            }
        },
        success: function(data){
        //alert('Data being returned from server: ' +data.toSource());
        //alert('Data being returned from server: ' +data.toSource());
        //alert(data);
        callbackData = data;

    }
    });

    return callbackData;
}
</code></pre>

<p>But, when I programatically change the webservice url to hold a wrong value, and upon calling the html page, I'm able to see an error message in the firebug console, but my snippet doesn't seem to be catching the error at all.</p>

<p>For e.g, While calling the GEONames API, I'm encountering an stating "407 Authorization required" in firebug's console.but even if I handle that status code in my error block, it is not firing.. What could be the reason?.</p>

<p>Don't we have any comprehensive solution for handling these HTTP errors effectively?.</p>
