简体   繁体   中英

Angularjs and HTTP headers

I would like to send my custom HTTP header to the REST service every time I make any request. I am using Apache HTTP Web Server. I have the following code:

app.config(['$httpProvider', function($httpProvider){

if(!$httpProvider.defaults.headers.get){
    $httpProvider.defaults.headers.get = {};
}

$httpProvider.defaults.headers.get['My-Token'] = 'some_test_token';
}]);

But I see next in the devtools:

在此输入图像描述

My header inline into other. What am I doing wrong?

The screenshot that you provided deals with the OPTIONS request, whereas you are configuring the GET request. An OPTIONS request is sent as part of the implementation of content negotiation in HTTP.

In this case, you are sending the preflight OPTIONS request, which in turn indicates that your future request will include the my-token header. This is desired behaviour, since the server can now instruct the client to not proceed with this request or perform a conditional action.

In your browser (or other software you're using to debug HTTP), look for the GET request and you should see your header included correctly.

The trouble wasn't at the AngularJS side. I just configure the Apache HTTP server and back-end part of my application. My Spring Security filter chain blocked OPTIONS requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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