How to handle error In $.get()

后端 未结 3 2174
长发绾君心
长发绾君心 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:20

    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" );
    });
    

提交回复
热议问题