简体   繁体   English

如何在Http请求标头中设置值

[英]How to Set value in Http Request Header

I am dealing with cross origin request to access my restful API's,so i want to set field "Authorization" in http request header on every request,but is not going to set in header...so what am doing wrong here? 我正在处理跨源请求以访问我的静态API,因此我想在每个请求的http请求标头中设置字段“授权”,但不会在标头中设置...所以这是怎么回事?

My app.js 我的app.js

'use strict';

var app = angular.module('samepinch', [
    'ngResource',
    'ngCookies',
    'ui.router',
    'toaster',
    'ui.bootstrap',
    'oc.lazyLoad',
    'samepinch.controllers',
    'samepinch.directives',
    'samepinch.factory',
    'samepinch.services',

    // Added in v1.3
    'FBAngular',
    'Config',
    'samepinch.login',
    'samepinch.item',
    'samepinch.common'
]);

angular.module('samepinch.login',[]);
angular.module('samepinch.item',[]);
angular.module('samepinch.common',[])

app.run(function($http)
{
      $http.defaults.headers.common.Authorization = '';
    // Page Loading Overlay
    public_vars.$pageLoadingOverlay = jQuery('.page-loading-overlay');

    jQuery(window).load(function()
    {
        public_vars.$pageLoadingOverlay.addClass('loaded');
    })
});

Controller is 控制器是

angular.module('samepinch.login').controller('LoginController',['$scope','LoginService','$rootScope','$http',function($scope,LoginService,$rootScope,$http){

    $rootScope.isLoginPage        = true;
    $rootScope.isLightLoginPage   = false;
    $rootScope.isLockscreenPage   = false;
    $rootScope.isMainPage         = false;

    $scope.register = function(credentials){
        $http.defaults.headers.common.Authorization = 'dfdfdf';
        LoginService.post(credentials,function(success){

        },function(error){

        });
    }

}]);

My Service is 我的服务是

'use strict';


angular.module('samepinch.login').factory('LoginService', ['$resource','$enviornment', function ($resource,$enviornment) {
    var url = $enviornment.backendurl;
     return $resource(url+'authenticate',{},{
         query: {
             method:'GET', 
             params:{itemId:''}, 
             isArray:true
         },
         post: {
             method:'POST',
             headers: {
                  'Authorization': 'Basic dGVzdDp0ZXN0',
                  'Content-Type': 'application/json'
            }
         },
         update: {
             method:'PUT', params: {itemId: '@entryId'}
         },
          remove: {
              method:'DELETE'
          }
     });
}]);

My Http request looks like 我的Http请求看起来像

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/api/v1/authenticate
Request Method:OPTIONS
Status Code:401 Unauthorized
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:9006
Pragma:no-cache
Referer:http://localhost:9006/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Response Headersview source
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE,PUT
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Wed, 22 Jul 2015 07:52:15 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block

So 'Authorization' field is not set in headers..please help me what am i missing here 因此,标题中未设置“授权”字段。请帮助我,我在这里缺少什么

This log is for OPTIONS request 该日志用于OPTIONS请求

Request Method:OPTIONS

First you need to setup your server to return 200 OK for OPTIONS call then the POST call with proper parameters will be send 首先,您需要将服务器设置为对OPTIONS调用返回200 OK ,然后将发送带有适当参数的POST调用

Look for instructions here http://enable-cors.org/ how to configure CORS (including OPTIONS request) on your architecture 在此处查找说明, 网址http://enable-cors.org/如何在您的体系结构上配置CORS(包括OPTIONS请求)

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

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