2014-01-09 86 views
4

根据http://docs.angularjs.org/api/ng.$http拦截器有以下方法:用例为requestError HTTP拦截

request: function(config) { 
    //we could manipulate query here 
    return config || $q.when(config); 
}, 
requestError: function(rejection) { 
    // what is the use case of this? 
    return $q.reject(rejection); 
}, 
response: function(response) { 
    // response.status === 200 
    return response || $q.when(response); 
}, 
responseError: function(rejection) { 
    // when response failed ... 
    return $q.reject(rejection); 
} 

如何requestError触发,哪些使用案例,你能想到的?

+0

在尝试发送请求失败后,如何显示连接错误消息? –

+1

@AndreyShustariov hm当$ http无法连接资源,因为资源处于脱机状态时,会触发responseError –

+0

好的。然后格式不正确的请求。例如,忘了设置url属性。 –

回答

3

一个有趣的用例是拆除/撤消在请求之前设置的东西,并且在响应之后将被撕掉。例子:

  • 负载指示
  • 覆盖
  • 禁用表单字段

现在,当请求不能被发送或者被另一拦截拒绝,requestError使您能够采取适当行动的机会并删除该加载指示器或启用表单域。