处理jQuery AJAX Web服务调用的http错误的问题

Issues with handling http errors with jQuery AJAX webservice calls

本文关键字:http 错误 问题 调用 服务 jQuery AJAX Web 处理      更新时间:2023-09-26

我正在开发一个jQuery应用程序,在该应用程序中,我需要在HTTP错误发生时捕获它。下面是我的片段。

// Function to validate URL
function validateURL(url) 
{
    var pattern = new RegExp();
    pattern.compile("^[A-Za-z]+://[A-Za-z0-9-_]+''.[A-Za-z0-9-_%&'?'/.=]+$");
    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') && (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 < 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;
}

但是,当我用程序更改Web服务url以保存错误的值时,在调用html页面时,我可以在firebug控制台中看到一条错误消息,但我的代码片段似乎根本没有捕捉到错误。

例如,在调用GEONames API时,我在firebug的控制台中遇到一个声明"需要407授权"。但即使我在错误块中处理该状态代码,它也不会启动。。原因可能是什么?。

难道我们没有任何全面的解决方案来有效地处理这些HTTP错误吗?。

我认为您的代码有一些问题。。。首先,handleError是如何调用的?因为你调用了一个名为handleError的方法,但什么都不传递。。。我假设你使用.ajax()

你应该这样做:

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    },
    500: function() {
      alert('server error');
    }
  },
  success : {
      alert('it working');
  },
  complete : {
      alert('im complete');
});