angular學習筆記(二十四)-$http(2)-設定http要求標頭

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   2014   cti   

1. angular預設的要求標頭:

其中,Accept 和 X-Requested-With是$http內建的預設配置

 

2. 修改預設要求標頭:

   (1) 全域修改(整個模組)

        使用$httpProvider依賴

        var myApp = angular.module(‘MyApp‘,[]);        myApp.config(function($httpProvider){             console.log($httpProvider.defaults.headers.common)                          //修改/操作$httpProvider.defaults.headers.common對象的屬性以改變$http的預設要求標頭配置
})

        *注意,只能操作 $httpProvider.defaults.headers.common 才有效,直接操作$httpProvider.defaults.headers是無效的.

   (2)  特定請求修改(某個http請求)

         直接在$http(config)的config參數中的headers項進行配置

 

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中的$http.get‘;app.get(‘/api/user‘,function(req,res){    res.send(data)});app.listen(3000);

(1).通過$httpProvider對整個模組的$http要求標頭進行修改:

var httpGet = angular.module(‘HttpGet‘,[]);httpGet.config(function($httpProvider){
    console.log($httpProvider.defaults.headers)
    //刪除後要求標頭裡不再有 X-Requested-With 屬性    delete $httpProvider.defaults.headers.common[‘X-Requested-With‘];
    //為要求標頭添加Authorization屬性為‘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).通過$http(config)的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‘}  //要求標頭裡會添加Authorization屬性為‘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()});

 

完整代碼地址: 

*注: 書上說到可以通過$httpProvider.defaults.headers.get[‘DNT‘]=‘1‘,來設定不追蹤使用者瀏覽資訊,但實際嘗試後發現是報錯的. 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.