开发者

How to handle error In $.get()

I have a jquery code in which I am using get() and calling some remote url/file. Now I want to know what the best way is to handle errors from this.

What I am doing is:

   $(document).ready(function() {
        $.ajaxSetup({
            error: function(x, e) {

                if (x.status == 0) {
                    alert(' Check Your N开发者_如何转开发etwork.');
                } 
                   else if (x.status == 404) {
                alert('Requested URL not found.');

                } else if (x.status == 500) {
                    alert('Internel Server Error.');
                }  else {
                    alert('Unknow Error.\n' + x.responseText);
                }
            }
        });

        $.get("HTMLPage.htm", function(data) {
            alert(data);
            $('#mydiv').html(data);

        });
    });

This is working fine.But want to know is there any better way of doing this?

ref:http://www.maheshchari.com/jquery-ajax-error-handling/


As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object. This jQuery XHR object, or "jqXHR," returned by $.get() implements the Promise interface, giving it all the properties, methods, and behavior of a Promise.

var jqxhr = $.get( "example.php", function() {
    alert( "success" );
})
.done(function() {
    alert( "second success" );
})
.fail(function() {
    alert( "error" );
})
.always(function() {
    alert( "finished" );
});


Using $.ajaxSetup is global for all ajax calls. Because the $.get function doesn't have any error callbacks, defining an error handler in $.ajaxSetup is the only way to handle errors. If you use $.ajax, you can define the error handler in the $.ajax call like this

$.ajax({
  url: "HTMLPage.htm",
  success: function(data) {
    alert(data);
    $('#mydiv').html(data);        
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) { 
    if (XMLHttpRequest.status == 0) {
      alert(' Check Your Network.');
    } else if (XMLHttpRequest.status == 404) {
      alert('Requested URL not found.');
    } else if (XMLHttpRequest.status == 500) {
      alert('Internel Server Error.');
    }  else {
       alert('Unknow Error.\n' + XMLHttpRequest.responseText);
    }     
  }
});

This is specific to only this ajax call, that way you can have more specific error messages. But using the global error handler works just as well.

You could define your functions outside of the $(document).ready() like this

$(document).ready(function() {
    $.ajaxSetup({
        error: AjaxError
    });

    $.get("HTMLPage.htm", GetSuccess);
});

function AjaxError(x, e) {
  if (x.status == 0) {
    alert(' Check Your Network.');
  } else if (x.status == 404) {
    alert('Requested URL not found.');
  } else if (x.status == 500) {
    alert('Internel Server Error.');
  }  else {
     alert('Unknow Error.\n' + x.responseText);
  }
}

function GetSuccess(data) {
  alert(data);
  $('#mydiv').html(data);
}


copy/paste from http://api.jquery.com/jQuery.ajax/:

statusCode(added 1.5) {}
A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

$.ajax({
  statusCode: {404: function() {
    alert('page not found');
  }
});

If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜