最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect that user is offline after an $http request - Stack Overflow

programmeradmin3浏览0评论

I'd like to test for the network connectivity (online/offline) after an unsuccessful request with the $http service in AngularJs. When looking for all the available response params in the error handler, I don't see anything that could help me.

Thanks for helping, Olivier

Code for the request:

$http({ method: method, url: url, data: data, headers: headers })
  .success(function (result) {
    deferred.resolve(result);
  })
  .error(function (data, status, headers, config, statusText) {
    console.log(data);
    console.log(status);
    console.log(headers);
    console.log(config);
    console.log(statusText);
    deferred.reject();
  });

Result in the console:

GET http://192.168.0.12:3000/test net::ERR_INTERNET_DISCONNECTED ionic.bundle.js:16185
 backEnd.js:25
0 backEnd.js:26
function (name) {
    if (!headersObj) headersObj =  parseHeaders(headers);

    if (name) {
      return headersObj[lowercase(name)] || null;
    }

    return headersObj;
  } backEnd.js:27
Object {method: "GET", transformRequest: Array[1], transformResponse: Array[1], url: "http://192.168.0.12:3000/test", data: null…} backEnd.js:28
undefined backEnd.js:29

I'd like to test for the network connectivity (online/offline) after an unsuccessful request with the $http service in AngularJs. When looking for all the available response params in the error handler, I don't see anything that could help me.

Thanks for helping, Olivier

Code for the request:

$http({ method: method, url: url, data: data, headers: headers })
  .success(function (result) {
    deferred.resolve(result);
  })
  .error(function (data, status, headers, config, statusText) {
    console.log(data);
    console.log(status);
    console.log(headers);
    console.log(config);
    console.log(statusText);
    deferred.reject();
  });

Result in the console:

GET http://192.168.0.12:3000/test net::ERR_INTERNET_DISCONNECTED ionic.bundle.js:16185
 backEnd.js:25
0 backEnd.js:26
function (name) {
    if (!headersObj) headersObj =  parseHeaders(headers);

    if (name) {
      return headersObj[lowercase(name)] || null;
    }

    return headersObj;
  } backEnd.js:27
Object {method: "GET", transformRequest: Array[1], transformResponse: Array[1], url: "http://192.168.0.12:3000/test", data: null…} backEnd.js:28
undefined backEnd.js:29
Share Improve this question edited Nov 9, 2014 at 11:12 olimungo asked Nov 9, 2014 at 10:34 olimungoolimungo 671 silver badge6 bronze badges 1
  • Try using the DOM online and offline events - developer.mozilla/en/docs/Online_and_offline_events – Adam Jenkins Commented Nov 9, 2014 at 11:15
Add a ment  | 

1 Answer 1

Reset to default 8

If you want to simply detect that you are online or not, you could use:

if (navigator.onLine) {
  alert('online')
} else {
  alert('offline');
}

For this and other online/offline checking techniques, check out: http://www.html5rocks./en/mobile/workingoffthegrid/

发布评论

评论列表(0)

  1. 暂无评论