2

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.

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

Thanks

Bhupi
  • 2,928
  • 6
  • 34
  • 51

2 Answers2

3

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

var is_online = false;

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

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

Check this Stack Overflow question.

Community
  • 1
  • 1
Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
  • 1
    This is a good answer, but from my experience the navigator.onLine property does not get updated after the page is loaded. So if the user's connectivity changes after they load up your app, you may not know about it. – fil maj Feb 16 '11 at 19:37
  • You can always test via another file using AJAX and return true if online, false otherwise; that new file will obviously always have the newest online state if it gets called from multiple positions in the original file, am I wrong here? – Anriëtte Myburgh Feb 19 '11 at 10:46
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:

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!

fil maj
  • 2,260
  • 16
  • 16