我可以获得用Ajax调用的函数的返回代码吗

Can I get the return code of a function called with Ajax

本文关键字:函数 返回 代码 调用 Ajax 我可以      更新时间:2023-09-26

可能重复:
可以';t从jQuery Ajax调用中获取正确的返回值

这是我之前问题的延伸:

我可以检查用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;
            })
}

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

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