Swa*_*ain 11 javascript timeout angularjs ionic-framework
我在角度js中使用$ http进行ajax调用.我已经实现了超时.但是,如果连接超时,我想向用户显示错误消息.以下是代码..
$http({
method: 'POST',
url: 'Link to be called',
data: $.param({
key:Apikey,
id:cpnId
}),
timeout : 5000,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(result){
alert(result);
}).error(function(data){
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以在连接超时时显示用户.有什么办法可以在一个地方配置吗?
Nic*_*nel 23
如果你想在每个请求的连接超时时做一些事情,你可以使用拦截器(全局超时参数不起作用):
// loading for each http request
app.config(function ($httpProvider) {
$httpProvider.interceptors.push(function ($rootScope, $q) {
return {
request: function (config) {
config.timeout = 1000;
return config;
},
responseError: function (rejection) {
switch (rejection.status){
case 408 :
console.log('connection timed out');
break;
}
return $q.reject(rejection);
}
}
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24273 次 |
| 最近记录: |