简体   繁体   English

AngularJS $ http自定义标头无效

[英]AngularJS $http custom headers not working

I'm trying to add a custom header to an HTTP request but AngularJS does not seem to handle it. 我正在尝试向HTTP请求添加自定义标头,但AngularJS似乎没有处理它。 Here is my code: 这是我的代码:

$http({
    method: "GET",
    url: 'http://localhost:8080/Schedule-service/login',
    headers: {
        'X-AUTHENTICATION': 'TRUE'
    }
}).success(function (response) {
    $cookies[constants.TOKEN] = response.hash;
}).error(function (data, status, headers, config) {

});

I have tried to add this to the default headers but still nothing. 我试图将其添加到默认标题但仍然没有。 What is wrong here? 这有什么不对?

Edit: My request in the middle of doing GET: 编辑:我在做GET的过程中的请求:

Request URL:http://localhost:8080/Schedule-service/login
Request Headers CAUTION: Provisional headers are shown.
Access-Control-Request-Headers:accept, x-authentication
Access-Control-Request-Method:GET
Origin:http://localhost
Referer:http://localhost/ScheduleWebClient/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/32.0.1700.76 Safari/537.36

And after GET: GET之后:

Request URL:http://localhost:8080/Schedule-service/login
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source

Request Headers:

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, x-authentication
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost
Referer:http://localhost/ScheduleWebClient/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/32.0.1700.76 Safari/537.36

Response Headers:

Access-Control-Allow-Headers:origin, content-type, accept, x-requested-with, sid,   mycustom, smuser
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Content-Length:0
Date:Fri, 24 Jan 2014 20:33:23 GMT
Server:GlassFish Server Open Source Edition 3.1.2.2
Set-Cookie:JSESSIONID=5f4fc0d1318a2e63cd1ca9170386; Path=/Schedule-service; HttpOnly
X-AUTHENTICATION:*
X-Powered-By:Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)

It seems you're performing CORS request and the request you pasted is preflight request. 您似乎正在执行CORS请求,并且您粘贴的请求是预检请求。 The application is working on port 80 but you're calling service on port 8080 hence its CORS cross origin. 该应用程序正在端口80上工作,但您在端口8080上调用服务,因此它的CORS交叉源。 In the response above you see that the server will accept x-requested-with header 在上面的响应中,您会看到服务器将接受x-requested-with标头

Access-Control-Allow-Headers:origin, content-type, accept, x-requested-with.

The header should be visible in the next request 标题应该在下一个请求中可见

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

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