AngularJS $http自定义标头无法工作

4
我正在尝试向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)

尝试使用发送到成功回调的 headers 参数(参数为 datastatusheadersconfig)。 - Eliran Malka
另外,HTTP头部的X-前缀现已被弃用(http://tools.ietf.org/html/draft-ietf-appsawg-xdash-05)。为什么不改用标准的`Authorization`头部呢? - Eliran Malka
1个回答

0

看起来你正在执行CORS请求,而你复制的请求是预检请求。应用程序正在端口80上运行,但你正在调用端口8080上的服务,因此它是CORS跨源。在上面的响应中,你可以看到服务器将接受x-requested-with标头。

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

头信息应该在下一个请求中可见


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接