How to use angularJS interceptor to only intercept specific http requests?

前端 未结 7 592
耶瑟儿~
耶瑟儿~ 2020-12-23 21:17

I know how to intercept ALL requests, but I only want to intercept requests from my resources.

Does anyone know how to do this?

services.config([\'$h         


        
7条回答
  •  别那么骄傲
    2020-12-23 21:34

    The only way I know of doing this it to just filter out the requests you want in the response handler.

    e.g.

    ...
    response: function(response) {
        if(response.config.url.startsWith('/api/')) {
            //Do your custom processing here
        }
    
        return response;
    }
    ...
    

    Polyfill for string.startsWith()

    //Taken from http://stackoverflow.com/questions/646628/javascript-startswith
    if (typeof(String.prototype.startsWith) === 'undefined') {
        String.prototype.startsWith = function(str) {
            return this.slice(0, str.length) === str;
        };
    }
    

提交回复
热议问题