简体   繁体   English

如何在$ http请求后检测到该用户是否处于脱机状态

[英]How to detect that user is offline after an $http request

I'd like to test for the network connectivity (online/offline) after an unsuccessful request with the $http service in AngularJs. 在AngularJs中使用$ http服务请求失败后,我想测试网络连接(在线/离线)。 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 感谢您的帮助,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

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.com/en/mobile/workingoffthegrid/ 有关此和其他在线/离线检查技术,请查看: http//www.html5rocks.com/en/mobile/workingoffthegrid/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM