【问题标题】:Can I get the return code of a function called with Ajax [duplicate]我可以获得使用 Ajax 调用的函数的返回码吗?
【发布时间】:2012-08-17 01:44:28
【问题描述】:

可能重复:
Can't get correct return value from an jQuery Ajax call

这是我之前问题的延伸:

我可以检查使用 Ajax 调用的函数的返回码吗,如下所示:

rc =  submitHandler($link, $modal);

function submitHandler($link, $modal) {

           $.ajax({
                url: oSubmit.href,
                dataType: 'json',
                type: 'POST',
                data: $form.serializeArray()
            })
            .done(function (json, textStatus, XMLHttpRequest) {
                json = json || {};
                if (json.success) {
                    submitSuccessModal(oSubmit, json);
                    return true;  <---------------------------------------
                } else {
                    submitFailModal(oSubmit, json);
                    return false;  <--------------------------------------
                }
                return false;
            })

}

【问题讨论】:

标签: javascript jquery ajax http-status-codes


【解决方案1】:

可以为每个状态码分别添加一个功能块

$.ajax({
  statusCode: {
    404: function() {
      alert("page not found");
    },
    500: function() {
      alert("internal server error")
    },
    ...
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2022-12-04
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多