简体   繁体   English

Angularjs和HTTP标头

[英]Angularjs and HTTP headers

I would like to send my custom HTTP header to the REST service every time I make any request. 我想在每次发出请求时将自定义HTTP标头发送到REST服务。 I am using Apache HTTP Web Server. 我正在使用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: 但我在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. 您提供的屏幕截图处理OPTIONS请求,而您正在配置GET请求。 An OPTIONS request is sent as part of the implementation of content negotiation in HTTP. OPTIONS请求作为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. 在这种情况下,您将发送预检OPTIONS请求,该请求反过来表明您将来的请求将包含my-token标头。 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. 在您的浏览器(或您用于调试HTTP的其他软件)中,查找GET请求,您应该看到正确包含的标头。

The trouble wasn't at the AngularJS side. 问题不在于AngularJS方面。 I just configure the Apache HTTP server and back-end part of my application. 我只是配置Apache HTTP服务器和我的应用程序的后端部分。 My Spring Security filter chain blocked OPTIONS requests. 我的Spring Security过滤器链阻止了OPTIONS请求。

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

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