2

在 Jquery Ajax 中,如果由于服务器关闭而找不到/无法加载 ajax url,我如何显示警报错误消息。

我试过了

error:function(){
}

statusCodes:{
}

只有在成功加载 url 时,这些才有效。如果 url 无法加载,我该如何显示错误消息?

我正在使用 JSONP

4

3 回答 3

1

您可以使用以下内容检查 jQuery ajax 返回的所有状态

 $.ajax(
   statusCode: {
      200: function (response) {
         alert('status 200');
      },
      201: function (response) {
         alert('status  201');
      },
      400: function (response) {
         alert('status  400');
      },
      404: function (response) {
         alert('status  404 ');
      }
   }, success: function () {
      alert('success');
   },
});
于 2013-03-12T04:30:33.427 回答
0

我在刚刚编辑的 ajax 调用中尝试了这个,我收到了警报。

                $.ajax({
                    url: "/Stores/Ajax_GetStoreDetailsw",
                    type: 'POST',
                    data: { StoreId: selectedStoreId },
                    success: function (data) {
                        $(".StoreWindows").html(data);
                    },
                    error: function () {
                        alert(9);
                        //alert the error here.
                    }
                });

我将网址更改为不正确的网址,并出现了警报。

也将您的错误处理程序更改为此,以便您可以开始查看哪些错误产生了哪些错误代码。

                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                        //alert the error here.
                    }
于 2013-03-12T04:19:15.963 回答
0

响应具有相应代码时要调用的数字 HTTP 代码和函数的对象。例如,当响应状态为 404 时会发出警报:Jquery ajax try like this。

$.ajax({
  statusCode: {
    200: function() {
      alert("OK ");
    },
    404: function() {
      alert("page not found");
    }
  }
});

尽管您也可以在错误块中获取状态代码。像这样的东西。

error: function (xhrReq, textstatus, errorThrown) {
                        alert(xhrReq.status);
                        alert(errorThrown);

                    }
于 2013-03-12T04:22:00.887 回答