angular.js - How to set global ajax request in angular?
迷茫
迷茫 2017-05-15 16:50:14
0
1
617

Need to add permission judgment and prompt for each request? If you have any newbie questions, please feel free to ask

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
大家讲道理

You'd better describe the problem in more detail, because the specific approach may vary depending on the situation.

Generally speaking, if you just want to customize Headers, you can use $httpProvider.

module.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.headers.common['ANYTHING'] = 'YOU_NEEDED';
}]);

Please note that the headers of post, patch, put have their own independent configuration parts (common is shared by all methods). In addition, the configuration in module.config is only valid during initialization. If you need to modify it during operation, just $http service:

$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w'

If you want to uniformly handle requests, responses, request errors and response errors, you need to use Interceptors. The code of this example is too wordy, so I won’t write it. You can check the $http service document yourself. To put it simply, you can use factory to create custom interceptors, and then add them to $httpProvider.interceptors (which is an array), so that these interceptors act like middleware for each request All are handled uniformly.

Finally, I have to say, either you describe the problem in more detail and accurately, or take the initiative to read the documentation. The documentation for $http and $httpProvider is only two pages, and everything you want to know is on it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template