Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在http拦截器中中止请求?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在http拦截器中中止请求?

Javascript 如何在http拦截器中中止请求?,javascript,angularjs,Javascript,Angularjs,在应用程序中,我有以下api url结构: // public public/url-xyz // private dashboard/url-xyz 知道这一点,并试图保存不必要的请求:取消请求的最佳方式是什么?到目前为止,我尝试的是: angular.module('mymod').factory('httpInterceptors', function ($injector, $q, httpBuffer) { return { request: functi

在应用程序中,我有以下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
出现问题,因为如果请求被取消,它需要一个数组并获取一个对象作为响应。

您应该能够返回
$q.reject([原因])


您应该能够返回
$q.reject([原因])


您应该能够返回
$q.reject([原因])


您应该能够返回
$q.reject([原因])


有趣的是,我通过谷歌找到了同一个问题,但我无法通过stacoverflow搜索找到,因此无法将我的问题标记为重复问题:这里的教程有趣的是,我通过谷歌找到了同一个问题,但我无法通过stacoverflow搜索找到它,因此无法将我的问题标记为重复:tutorial here有趣我通过google找到了相同的问题,但我无法通过stacoverflow搜索找到它,因此无法将我的问题标记为重复:tutorial here有趣我通过google找到了相同的问题,但是我无法通过stacoverflow搜索找到它,因此我无法将我的问题标记为重复:教程在这里起作用,但也使用拦截器破坏了许多第三方模块(例如重新启动、各种全局加载微调器、http身份验证模块等),拦截器将截获所有
$http
请求,如果您拒绝它们,将产生影响。有些工具不够智能,无法查找被拒绝的承诺,并且总是假设请求将有成功的响应。这可以工作,但也会破坏许多使用拦截器的第三方模块(例如重新启动、各种全局加载微调器、http身份验证模块等),拦截器将截获所有
$http
请求,如果您拒绝它们,将产生影响。有些工具不够智能,无法查找被拒绝的承诺,并且总是假设请求将有成功的响应。这可以工作,但也会破坏许多使用拦截器的第三方模块(例如重新启动、各种全局加载微调器、http身份验证模块等),拦截器将截获所有
$http
请求,如果您拒绝它们,将产生影响。有些工具不够智能,无法查找被拒绝的承诺,并且总是假设请求将有成功的响应。这可以工作,但也会破坏许多使用拦截器的第三方模块(例如重新启动、各种全局加载微调器、http身份验证模块等),拦截器将截获所有
$http
请求,如果您拒绝它们,将产生影响。有些工具不够聪明,无法寻找被拒绝的承诺,并且总是假设请求将得到成功响应。
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);
        }       
    };
});