1. angular Default request header :

among ,Accept and X-Requested-With yes $http Built in default configuration

Accept:application/json,text/plain         The data type of the request accepted : json Text

X-Requested-With: XMLHttpRequest     Request is through XMLHttpRequest Object

2. Modify the default request header :

(1) Global modification ( The whole module )

Use $httpProvider rely on

 var myApp = angular.module('MyApp',[]);
myApp.config(function($httpProvider){
console.log($httpProvider.defaults.headers.common)
// modify / operation $httpProvider.defaults.headers.common Object to change $http Default request header configuration for
})

* Be careful , You can only operate  $httpProvider.defaults.headers.common It works , Direct manipulation $httpProvider.defaults.headers It's invalid .

(2)   Specific request modification ( Some http request )

Directly in $http(config) Of config In the parameter headers Item to configure

demo:

html:

<!DOCTYPE html>
<html ng-app = 'HttpGet'>
<head>
<title>18.2 $http(1)</title>
<meta charset="utf-8">
<script src="angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller = "dataController">
<span>{{data}}</span>
</div>
</body>
</html>

nodejs:

var express = require('express');
var app = express();
app.use(express.static(__dirname+'')); var data = 'angularjs Medium $http.get'; app.get('/api/user',function(req,res){
res.send(data)
}); app.listen(3000);

(1). adopt $httpProvider For the whole module $http Request header to modify :

var httpGet = angular.module('HttpGet',[]);
httpGet.config(function($httpProvider){
 console.log($httpProvider.defaults.headers)
 // After deletion, there is no more... In the request header X-Requested-With attribute 
delete $httpProvider.defaults.headers.common['X-Requested-With'];
 // Add... To the request header Authorization The attribute is '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). adopt $http(config) Of config Parameter to configure the request header of the request :

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 request header will add Authorization The attribute is '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()
});

Full code address :https://github.com/OOP-Code-Bunny/angular/tree/master/OREILLY/18.3%20%24http(1)

* notes : The book says it can be done through $httpProvider.defaults.headers.get['DNT']='1', To set not to track user browsing information , But after the actual attempt, it was found that the report was wrong . 

angular Learning notes ( Twenty-four )-$http(2)- Set up http More related articles in the request header

  1. angular Learning notes ( hexacosa- )-$http(4)- Set request timeout

    This article mainly explains $http(config) Of config Medium timeout term : $http({ timeout: number }) The number , Starting with the request , The number of milliseconds to wait , No response beyond this number , Error is returned ...

  2. angular Learning notes ( Twenty-eight - attach 2)-$http,$resource Medium promise object

    The following promise Usage of , I started with the first one $http Notes to $resource In the notes , It works all the time until : HttpREST.factory('cardResource',function($resource) ...

  3. angular Learning notes ( Twenty-nine )-$q service

    angular Medium $q It's used to handle asynchronous ( The main thing, of course, is http Interactive ~). $q It's using promise Asynchronous programming based on . What is? promise Asynchronous programming ? The most important core of asynchronous programming is callback , Because there are callback functions , That's why ...

  4. angular Learning notes ( Twenty-eight - attach 1)-$resource The resource approach in

    adopt $resource Resources acquired , Or through $resource Instantiated resources , The resource itself has some methods ,$save,$delete,$remove, It can be called directly to save the resource : Let's say I have a $resour ...

  5. angular Learning notes ( Twenty-eight )-$http(6)- Use ngResource Module building RESTful framework

    ngResource The module is angular Specially for RESTful A module designed for Architecture , It provides '$resource' modular ,$resource Module is based on $http An encapsulation of . Let's take a look at its detailed usage 1. introduce ...

  6. angular Learning notes ( twenty-five )-$http(3)- Transform request and response formats

    This article mainly explains $http(config) Of config Medium tranformRequest Xiang He transformResponse term 1. transformRequest: $http({ transfo ...

  7. angular Learning notes ( Twenty-two )-$http.post

    Basic grammar : $http.post('url',{},{}).success(function(data,status,headers,config){ }).error(function(data,s ...

  8. angular Learning notes ( twenty )- Form validation

    This paper mainly introduces angular Form validation in : Form validation mainly includes the following contents : 1. required Instructions : amount to html5 Of required attribute , Validation cannot be empty 2. ng-maxlength attribute : Within validation ...

  9. angular Learning notes ( thirty )- Instructions (2)-restrice,replace,template

    This chapter is mainly about restrict attribute , replace attribute , template attribute These three attributes One . restrict: character string . Define how instructions are used in the view , There are four ways to use it : 1. Elements : ...

Random recommendation

  1. [Java] JSP note - Custom tag

    Steps to create a custom label : Four functions of custom tags : Class structure for custom tags : stay 1.0 What about China? , Can be <body-content> Is set to JSP, 2.0 It is not allowed to appear in the custom label body j ...

  2. C Language first 7 Homework

    1 #include<stdio.h> int main() { char name[50];int character[26]={0};int i=0,j;int length=0;wh ...

  3. java Branch statements in -- Classification of program operation process ( Sequential structure , Branching structure , Loop structure )

    JAVA There are three types of program operation process : 1, Sequential structure : Sequential structure is to execute each line of code in turn 2, Branching structure : Branch structure is to branch according to different conditions 3, Loop structure : A piece of code circulates according to conditions . among , There are two types of branching structures : ...

  4. Git Basic commands ( One )

    ( One ) For configuration information Git command 1. View properties of local settings  git config user.name/user.email 2. Display local configuration information  git config -l( Lowercase L) 3. Modify the current full ...

  5. 3、 ... and GPU The computing architecture of parallel programming

    Preface GPU How to achieve parallel ? It's done in a way that's better than CPU What's the difference between multithreading and multithreading ? This article will do a more detailed analysis . GPU Parallel computing architecture GPU The core of parallel programming is threads , A thread is a single instruction stream in a program , ...

  6. java.lang.SecurityException: class &quot;javax.servlet.FilterRegistration&quot;(spark Next maven)

    Today I write spark The example uses maven, But myself maven I'm not familiar with . I've been looking for mistakes for a long time and I know it's (sevlet-api2.5 3.0) Package conflict needs to solve the dependency problem between packages, but I don't know how to start . But in the end, it's time to find a novice ...

  7. How to diagnose application service performance problems (Tomcat、Weblogic middleware )

    In us web In the project , Our common web The application server has Tomcat.Weblogic.WebSphere. They are the infrastructure software of Internet application system , Also called “ middleware ”, Responsible for handling dynamic page requests , And give the app a name . Business ...

  8. U - god 、 God and God ( Second season water )

    Description HDU 2006'10 ACM contest The awarding Party of has begun !         To enliven the atmosphere , The organizers held a special meeting . A lottery with lots of prizes , The specific requirements of this activity are as follows :  ...

  9. python Regular implementation simple calculator

    Using regular implementation calculator Using regularization to realize the function of simple calculator , Be able to design and calculate addition, subtraction, multiplication and division operations with brackets . Of course not eval Such statements . Recursion : import re from functools import reduc ...

  10. J2EE Advanced ( One ) Lecture notes

    Enter the company for R & D , Basically, it's all J2EE frame . If you want to get a good position in the company , First of all, we must pass the technical standard . Due to demand , I learned by myself J2EE A span , Mainly Struts2,MyBatis(Hibernate The forerunner of ), Far away ...