在没有网络可用且Android上禁用wifi时处理ajax请求

时间:2021-09-19 16:57:32

I am developing an Android Application using phonegap. Everything is working fine as request/response, now I have to handle the situation when no 3G network is available and wifi is disabled, in this case my jquery ajax() request does not do anything it does not come to error handler even. I have put the timeout of 10seconds.

我正在使用phonegap开发Android应用程序。一切都工作得很好作为请求/响应,现在我必须处理没有3G网络可用和wifi被禁用的情况,在这种情况下我的jquery ajax()请求不做任何事情它甚至没有来到错误处理程序。我把10秒的超时时间。

Can somebody tell me what is going wrong with ajax request? why the error handler is not calling?

有人能告诉我ajax请求出了什么问题吗?为什么错误处理程序没有调用?

Thanks

谢谢

2 个解决方案

#1


2  

Use PhoneGap's Network API to test for the current state of network connectivity on the device. Here's the example taken from the documentation page linked above:

使用PhoneGap的网络API测试设备上当前的网络连接状态。以下是上面链接的文档页面中的示例:

function reachableCallback(reachability) {
  // There is no consistency on the format of reachability
  var networkState = reachability.code || reachability;

  var states = {};
  states[NetworkStatus.NOT_REACHABLE]                      = 'No network connection';
  states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
  states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK]         = 'WiFi connection';

  alert('Connection type: ' + states[networkState]);
}

navigator.network.isReachable('phonegap.com', reachableCallback);

Hope that helps!

希望有所帮助!

#2


3  

You can always do a test beforehand, before calling $(document).ready() and set a variable to false:

在调用$(document).ready()并将变量设置为false之前,您总是可以事先进行测试:

var is_online = false;

if (navigator.onLine(connected)) {
    is_online = true;
}

And then you can test for that variable in your $.ajax() request.

然后,您可以在$ .ajax()请求中测试该变量。

Check this Stack Overflow question.

检查此Stack Overflow问题。

#1


2  

Use PhoneGap's Network API to test for the current state of network connectivity on the device. Here's the example taken from the documentation page linked above:

使用PhoneGap的网络API测试设备上当前的网络连接状态。以下是上面链接的文档页面中的示例:

function reachableCallback(reachability) {
  // There is no consistency on the format of reachability
  var networkState = reachability.code || reachability;

  var states = {};
  states[NetworkStatus.NOT_REACHABLE]                      = 'No network connection';
  states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
  states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK]         = 'WiFi connection';

  alert('Connection type: ' + states[networkState]);
}

navigator.network.isReachable('phonegap.com', reachableCallback);

Hope that helps!

希望有所帮助!

#2


3  

You can always do a test beforehand, before calling $(document).ready() and set a variable to false:

在调用$(document).ready()并将变量设置为false之前,您总是可以事先进行测试:

var is_online = false;

if (navigator.onLine(connected)) {
    is_online = true;
}

And then you can test for that variable in your $.ajax() request.

然后,您可以在$ .ajax()请求中测试该变量。

Check this Stack Overflow question.

检查此Stack Overflow问题。