简体   繁体   English

所有请求的AngularJS $ http自定义标头

[英]AngularJS $http custom header for all requests

I was wondering if there is any way to configure all $http requests header with adding custom info. 我想知道是否有任何方法可以通过添加自定义信息来配置所有$ http请求标头。 Something like config : 像配置的东西:

 var config = {headers: {
            'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
            'Accept': 'application/json;odata=verbose'
        }
    };

But for all $http calls I will make in different services. 但对于我将在不同服务中进行的所有$ http呼叫。 I'm sure there is a solution :D.Thanks 我确信有一个解决方案:D。谢谢

You can create a $http interceptor to extend your header: 您可以创建$http拦截器来扩展标头:

 myapp.factory('httpRequestInterceptor', function () { return { request: function (config) { config.headers['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ=='; config.headers['Accept'] = 'application/json;odata=verbose'; return config; } }; }); myapp.config(function ($httpProvider) { $httpProvider.interceptors.push('httpRequestInterceptor'); }); 

A simpler solution could be to use Angular's run block: 一个更简单的解决方案可能是使用Angular的run块:

app.run(['$http', function ($http) {
    $http.defaults.headers.common['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
    $http.defaults.headers.common['Accept'] = 'application/json;odata=verbose';
}]);

Note: This solution allows you to pass the static value only one time since the run block executes only once. 注意:此解决方案允许您仅传递静态值一次,因为run块仅执行一次。

  use the folllowing code and  you can also control  $http timeout from 
 config setting.    
 'use strict';
   var app = angular.module('b2capp', []);
   var apiRequestCount = 0;      
   app.config(function ($httpProvider) {
      $httpProvider.interceptors.push(function ($rootScope, $q) {
       return {
           request: function (config) {
               apiRequestCount++;
                //   config.timeout =300000;
              return config;
          },
          response: function (response) {
               return response;
          },
           responseError: function (rejection) {
             switch (rejection.status) {
                   case 408:
                        console.log('connection timed out');
                        break;
              }
               // return $q.reject(rejection);
               return rejection;
            }
          }
       })
    });
 }]);
   app.controller('myCtrl', function ($scope, $http, $timeout) {
    var headers = {
            //'Authorization': 'Basic ' + btoa(username + ":" + password),
               'Access-Control-Allow-Origin': true,
               'Content-Type': 'application/json; charset=utf-8',
               "X-Requested-With": "XMLHttpRequest"
                 }
    $http.post(url + 'Search_6e', reqCookie, {
                   headers
            })
            .then(function Success(response) {               
                 $scope.myData = resultData;
                 console.log($scope.myData);
           }, function myError(response) {
              //error code
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM