在getJSON中为ajax调用编写失败案例

时间:2016-07-14 08:01:01

标签: javascript jquery ajax asp.net-mvc getjson

我有下面的ajax调用代码,我想在失败的ajax调用上编写自定义错误。在下面的代码块中,我可以写同样的内容:

$.getJSON("/ProductMatrix/CaptureCategoryTypeList/" + $("#categoryFilter > option:selected").attr("value"),
            function (data) {
                var items = "<option> Default  </option>";
                $.each(data,
                    function (i, captureCategoryType) {
                        items += "<option value=' " + captureCategoryType.Value + "'>" + captureCategoryType.Text + "</option>";
                    });
                $("#categoryTypeFilter").html(items);
                SherlockAdmin.Shared.closeProcessingWheel();
            });

1 个答案:

答案 0 :(得分:1)

它就像

一样简单
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON( "example.json", function() {
  console.log( "success" );
})
  .done(function() {
    console.log( "second success" );
  })
  .fail(function() {
    console.log( "error" );
  })
  .always(function() {
    console.log( "complete" );
  });

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
  console.log( "second complete" );
});

来自http://api.jquery.com/jquery.getjson/的代码