How to handle error In $.get()

后端 未结 3 2169
长发绾君心
长发绾君心 2020-12-28 15:40

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

3条回答
  •  情歌与酒
    2020-12-28 16:07

    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.

提交回复
热议问题