警告JQuery Ajax状态码



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

我试着

error:function(){
}

statusCodes:{
}

这些只有在url加载成功时才有效。如果url加载失败,如何显示错误信息?

我正在使用JSONP

您可以使用以下代码检查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');
   },
});

我在一个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.
                    }
                });

我将url更改为不正确的url,并出现警告。

将错误处理程序也更改为this,这样您就可以开始查看哪些错误产生了哪些错误代码。

                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                        //alert the error here.
                    }

一个数字HTTP代码和函数的对象,当响应具有相应的代码时将调用该对象。例如,当响应状态为404时,下面的代码将发出警报像这样试试。

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

虽然您也可以在错误块中获得状态码。像这样的东西。

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

最新更新