AngularJS $ http自定义标头无效

时间:2014-01-24 18:47:55

标签: angularjs rest header

我正在尝试向HTTP请求添加自定义标头,但AngularJS似乎无法处理它。这是我的代码:

$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) {

});

我试图将此添加到默认标头但仍然没有。这有什么不对?

编辑: 我在做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

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)

1 个答案:

答案 0 :(得分:0)

您似乎正在执行CORS请求,而您粘贴的请求是预检请求。该应用程序正在使用端口80,但您在端口8080上调用服务,因此它的CORS交叉源。在上面的响应中,您会看到服务器将接受x-requested-with header

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

标题应该在下一个请求中可见