Angular Study Notes (24)-$ HTTP (2)-set the HTTP Request Header

Source: Internet
Author: User

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. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.