在app中,我有以下api的url结构:
// public
public/url-xyz
// private
dashboard/url-xyz
了解并尝试保存不必要的请求:取消请求的最佳方法是什么?我到目前为止所尝试的是:
angular.module('mymod').factory('httpInterceptors', function ($injector, $q, httpBuffer)
{
return {
request: function (config)
{
var url = config.url;
if (url.match('/dashboard/')) {
// immediately cancel request
var canceler = $q.defer();
config.timeout = canceler.promise;
canceler.reject(config);
// logout and go to login-page immediately
// ...
}
// request config or an empty promise
return config || $q.when(config);
}
};
});
但这会导致$ressource
出现问题,因为它需要一个数组并将对象作为响应获取,如果它的请求被取消的话。
答案 0 :(得分:2)
您应该可以返回$q.reject([reason])
angular.module('mymod').factory('httpInterceptors', function ($injector, $q, httpBuffer)
{
return {
request: function (config)
{
var url = config.url;
if (url.match('/dashboard/')) {
// immediately cancel request
return $q.reject(config);
// logout and go to login-page immediately
// ...
}
// request config or an empty promise
return config || $q.when(config);
}
};
});