zoukankan      html  css  js  c++  java
  • Should I use .done() and .fail() for new jQuery AJAX code instead of success and error

    Should I use .done() and .fail() for new jQuery AJAX code instead of success and error

    I have coded like this:

    $.ajax({ cache: false,
        url: "/Admin/Contents/GetData",
        data: { accountID: AccountID },
        success: function (data) {
            $('#CityID').html(data);
        },
        error: function (ajaxContext) {
            alert(ajaxContext.responseText)
        }
    });
    

    But when I look at the jQuery .ajax() documentation at the end it seems to suggest I should be coding like this below or at least it suggests adding a .done() and a .fail():

    var request = $.ajax({ cache: false,
        url: "/Admin/Contents/GetData",
        data: { accountID: AccountID }
    });
    
    request.done(function (data) {
        xxx;
    });
    request.fail(function (jqXHR, textStatus) {
        xxx;
    });
    

    Update

    If I code like this is it the same or is there some advantage to breaking it into three ?

    $.ajax({ cache: false,
        url: "/Admin/Contents/GetData",
        data: { accountID: AccountID }
    }).done(function (data) {
        xxx;
    }).fail(function (jqXHR, textStatus) {
        xxx;
    });
    

    回答:

    As stated by user2246674, using success and error as parameter of the ajax function is valid.

    To be consistent with precedent answer, reading the doc :

    Deprecation Notice:

    The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

    If you are using the callback-manipulation function (using method-chaining for example), use .done(), .fail() and .always() instead of success(), error() and complete().

  • 相关阅读:
    js兼容性——获取当前浏览器窗口的宽高
    pip 换源
    5 二分查找 算法
    顺序查找 冒泡 快排 等
    4 顺序表和链表
    python垃圾回收机制
    3 栈 队列 双头队列
    2 数据结构的性能分析 timeit
    1 时间复杂度
    线程池 爬取一本小说
  • 原文地址:https://www.cnblogs.com/chucklu/p/14504183.html
Copyright © 2011-2022 走看看