Want to use the Angularjs in the HTPP to send a request to the background, now there is a user uniquely identified tokens want to put in headers inside, that is {headres:{' token ': 1}}
The following JS is introduced in index.html:
Angular.module ('App.factorys', []). Factory ('Httpinterceptor',['$q','$injector','$localStorage', function ($q, $injector, $localStorage) {varHttpinterceptor = { 'Responseerror': Function (response) {// ...... return$q. Reject (response); }, 'Response': Function (response) {if(Response.Status = =21000) { //Console.log (' Do something ... '); } returnResponse | |$q. When (response); }, 'Request': function (config) {config.headers= Config.headers | | {}; if($localStorage. Token) {Config.headers.token=$localStorage. Token; //config.headers[' x-access-token ' = $localStorage. Token; }; returnConfig | |$q. When (config); returnconfig; }, 'Requesterror': function (config) {// ...... return$q. Reject (config); } }; returnHttpinterceptor; }])
After injecting factory into the app, configure it in config
. config (['$httpProvider', function () { $httpProvider. Interceptors.push ( Httpinterceptor);}])
Angularjs HTTP Settings Headers (user uniquely identifies token)