当internet连接丢失时,如何从Ajax调用中获取信息

时间:2022-10-18 07:41:41

I have the following:

我有以下几点:

 $.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        alert(ajaxContext.responseText)
    }
});

When I lose connectivity to the internet the error is called but I don't see anything in the responseText.

当我连接到internet时,这个错误会被调用,但是我在responseText中看不到任何东西。

Is there a way I can find out different kind of errors based on status information in the returned ajaxContent? I would really like to be able to put out a message saying "Internet connectivity lost" and another message if there is some other problem.

是否有办法根据返回的ajaxContent中的状态信息找出不同类型的错误?我真的很想发一条信息说“网络连接丢失了”,如果有其他问题的话,再发一条。

4 个解决方案

#1


2  

According to the jQuery docu the error function receives three arguments:

根据jQuery文档,错误函数接收三个参数:

  1. jqXHR:
  2. jqXHR:
  3. textStatus: a string describing the type of error that occurred
  4. 描述发生的错误类型的字符串
  5. errorThrown: an optional exception object, if one occurred
  6. 错误抛出:如果发生了一个可选的异常对象。

Furthermore it states:

此外它州:

Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

第二个参数(除了null)的可能值是“timeout”、“error”、“abort”和“parsererror”。当出现HTTP错误时,errorthrow接收HTTP状态的文本部分,如“未找到”或“内部服务器错误”。

So you might want to have a look at the content of the second adn third parameter.

你可能想看看第二个adn第三个参数的内容。

#2


1  

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        if(ajaxContext.status=="404")
         {
          //write your not found handler code here
         }
        else
        alert(ajaxContext.status)
    }
});

#3


1  

Updated.

更新。

You should just add: timeout: , somewhere within $.ajax({}). Also, cache: false, might help in a few scenarios.

您只需添加:timeout:,在$.ajax({})范围内的某个地方。此外,cache: false在某些情况下可能会有所帮助。

$.ajax is well documented, you should check options there, might find something useful.

美元。ajax有很好的文档说明,您应该检查那里的选项,可能会发现一些有用的东西。

JQuery Ajax - How to Detect Network Connection error when making Ajax call

JQuery Ajax—如何在调用Ajax时检测网络连接错误

#4


0  

if you mean defferent kinds of response results it's here - http://api.jquery.com/jQuery.ajax/ - statusCode parameters

如果你指的是不同类型的响应结果,它在这里——http://api.jquery.com/jQuery.ajax/ - statusCode参数

#1


2  

According to the jQuery docu the error function receives three arguments:

根据jQuery文档,错误函数接收三个参数:

  1. jqXHR:
  2. jqXHR:
  3. textStatus: a string describing the type of error that occurred
  4. 描述发生的错误类型的字符串
  5. errorThrown: an optional exception object, if one occurred
  6. 错误抛出:如果发生了一个可选的异常对象。

Furthermore it states:

此外它州:

Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

第二个参数(除了null)的可能值是“timeout”、“error”、“abort”和“parsererror”。当出现HTTP错误时,errorthrow接收HTTP状态的文本部分,如“未找到”或“内部服务器错误”。

So you might want to have a look at the content of the second adn third parameter.

你可能想看看第二个adn第三个参数的内容。

#2


1  

$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        if(ajaxContext.status=="404")
         {
          //write your not found handler code here
         }
        else
        alert(ajaxContext.status)
    }
});

#3


1  

Updated.

更新。

You should just add: timeout: , somewhere within $.ajax({}). Also, cache: false, might help in a few scenarios.

您只需添加:timeout:,在$.ajax({})范围内的某个地方。此外,cache: false在某些情况下可能会有所帮助。

$.ajax is well documented, you should check options there, might find something useful.

美元。ajax有很好的文档说明,您应该检查那里的选项,可能会发现一些有用的东西。

JQuery Ajax - How to Detect Network Connection error when making Ajax call

JQuery Ajax—如何在调用Ajax时检测网络连接错误

#4


0  

if you mean defferent kinds of response results it's here - http://api.jquery.com/jQuery.ajax/ - statusCode parameters

如果你指的是不同类型的响应结果,它在这里——http://api.jquery.com/jQuery.ajax/ - statusCode参数