Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在http get请求angularjs中添加自定义头。_Jquery_Ajax_Angularjs_Angularjs Resource_Angularjs Http - Fatal编程技术网

Jquery 在http get请求angularjs中添加自定义头。

Jquery 在http get请求angularjs中添加自定义头。,jquery,ajax,angularjs,angularjs-resource,angularjs-http,Jquery,Ajax,Angularjs,Angularjs Resource,Angularjs Http,这是我的angularjs请求 var req = { method: 'GET', url: 'http://localhost:8080/test', headers: { "x-auth-token" : user.token } } $http(req).success(f

这是我的angularjs请求

var req = {
                method: 'GET',
                url: 'http://localhost:8080/test',
                headers: {
                    "x-auth-token" : user.token
                }
            }

            $http(req).success(function(){
                console.log("yes you have done it");
            }).error(function(){
                console.log("oopsss");
            });
我在调用此请求时遇到了此异常

var req = {
                method: 'GET',
                url: 'http://localhost:8080/test',
                headers: {
                    "x-auth-token" : user.token
                }
            }

            $http(req).success(function(){
                console.log("yes you have done it");
            }).error(function(){
                console.log("oopsss");
            });
无法加载XMLHttpRequest。无效的HTTP 状态代码403

然而,在谷歌chrome的邮递员测试中,它运行良好,并按照我的预期返回响应。我把这个当成了

postman测试中的请求头

GET /appraisal HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: no-cache
x-auth-token: 123456
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
和在响应头中

Access-Control-Allow-Origin:*
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Fri, 27 Mar 2015 16:13:46 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
但是当我调用get请求时,浏览器会显示如下http请求头

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, x-auth-token
Access-Control-Request-Method:GET
Connection:keep-alive
Host:52.11.111.128:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/james/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36
http响应头是

Access-Control-Allow-Headers:x-auth-token
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Fri, 27 Mar 2015 17:03:16 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
我已在服务器中启用交叉请求源策略

如何正确请求标头

如何在控制器中解决此问题

请引导我朝这个方向走


提前感谢。

我们使用一个服务,为所有请求针对REST-API设置头。服务设置了授权标头,如下所示:

app.factory('api', function($http) {
    function init(token) {
        $http.defaults.headers.common.Authorization = token || 'Basic xyztoken==';
    }
    return {
        init : init
    };
});
在app.run上,我们初始化标题:

app.run(function(api, …) {
    …
    api.init();
    …
});
在AuthService上,我们使用一个新令牌设置授权标头:

app.factory('AuthService', function(api, …) {
    …
    // some login action with a response from the REST-API and 
    // set the new authorization header
    api.init('Bearer ' + response.data.accessToken);
    // some more stuff
    …
});
在我们的控制器中,您不需要使用自定义标头

只有当你使用它时,它才起作用。因此,您必须在应用程序的配置阶段设置withCredential标志:

app.config(function($httpProvider, …) {
    …
    $httpProvider.defaults.withCredentials = true;
    …
});
再见
拉尔夫

你好,拉尔夫谢谢你的快速回复,我试过这个,它很有效。然而,我得到了相同的错误403禁止。我该怎么办?嗨,詹姆斯,抱歉耽搁了。您必须为您的请求使用凭据。我已经更新了我的答案。嘿,戴夫,谢谢,没问题。但我还是错过了一些东西。没问题,我很快就会处理掉的。