javascript - 在 getJSON 中编写 ajax 调用的失败案例

标签 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();
            });

最佳答案

就这么简单

// 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/

关于javascript - 在 getJSON 中编写 ajax 调用的失败案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368648/

相关文章:

javascript - 无法使用 Jquery Ajax post 方法将 json 数据显示为 html

javascript - 找到一个不等于这个类的 div

javascript - 获取所选选项数据

javascript - mdl-stepper javascript 和 HTML

javascript - Rails 4 + jQuery : PUT ajax request

javascript - jQuery UI 自动完成显示 AJAX 源的标签和值

jquery - 使用jquery每10秒切换一次div?

jquery - ASP.NET MVC - Ajax 将空值传递给 Controller

javascript - 无法在 Javascript 中分配小数变量 [CRM Online 2016]

php - 不断刷新聊天内容