[英]How to detect that user is offline after an $http request
在AngularJs中使用$ http服務請求失敗后,我想測試網絡連接(在線/離線)。 在錯誤處理程序中查找所有可用的響應參數時,我沒有看到任何可以幫助我的內容。
感謝您的幫助,Olivier
請求代碼:
$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();
});
控制台中的結果:
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 (navigator.onLine) {
alert('online')
} else {
alert('offline');
}
有關此和其他在線/離線檢查技術,請查看: http : //www.html5rocks.com/en/mobile/workingoffthegrid/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.