1. Default angular request header:
Here, accept and X-requested-with are the default configurations of $ HTTP.
2. Modify the default request header:
(1) Global modification (entire module)
Use $ httpprovider dependency
VaR MyApp = Angular. module ('myapp', []); MyApp. config (function ($ httpprovider) {console. log ($ httpprovider. defaults. headers. common) // Modify/operate $ httpprovider. defaults. headers. attribute of the common object to change the default request header configuration of $ HTTP
})
* Note: Only $ httpprovider. defaults. headers. Common Operations are valid. The direct operation $ httpprovider. defaults. headers is invalid.
(2) modify a specific request (an HTTP request)
Configure headers directly in the config parameter of $ HTTP (config ).
Demo:
HTML:
<!DOCTYPE html>
Nodejs:
VaR express = require ('express '); var APP = Express (); app. use (Express. static (_ dirname + ''); var DATA = $ HTTP in angularjs. get '; app. get ('/API/user', function (req, Res) {res. send (data)}); app. listen (0, 3000 );
(1). Use $ httpprovider to modify the $ HTTP request header of the entire module:
var httpGet = angular.module(‘HttpGet‘,[]);httpGet.config(function($httpProvider){
console.log($httpProvider.defaults.headers)
// After deletion, the X-requested-with attribute Delete $ httpprovider. defaults. headers. Common ['x-requested-with'] will no longer exist in the Request Header;
// Add the authorization attribute to the Request Header as 'Code _ bunny'
$ Httpprovider. defaults. headers. Common ['authorization'] = 'Code _ bunny ';
});httpGet.factory(‘getData‘,function($http,$q){ return function(){ var defer = $q.defer(); $http({ method:‘get‘, url:‘/api/user‘ }).success(function(data,status,headers,config){ defer.resolve(data); }).error(function(data,status,headers,config){ defer.reject(data) }); return defer.promise }});httpGet.controller(‘dataController‘,function($scope,getData){ $scope.data = getData()});
(2). Configure the request header Through the config parameter of $ HTTP (config:
VaR httpget = Angular. module ('httpget', []); httpget. factory ('getdata', function ($ HTTP, $ q) {return function () {var defer = $ Q. defer (); $ HTTP ({method: 'get', URL: '/API/user', headers: {'authorization ': 'Code _ bunny '} // The authorization attribute will be added to the Request Header as 'Code _ bunny '}). success (function (data, status, headers, config) {defer. resolve (data );}). error (function (data, status, headers, config) {defer. reject (data)}); Return defer. promise}); httpget. controller ('datacontroller', function ($ scope, getdata) {$ scope. data = getdata ()});
Complete Code address:
* Note: We can use $ httpprovider. defaults. headers. get ['dnt '] = '1' to set not to trace user browsing information. However, an error is reported after the actual attempt.