AngularJS - 为GET请求设置HTTP标头

时间:2021-03-12 16:54:35

May be this is a general issue, which can be available on internet, But what I got is here.

可能这是一个普遍的问题,可以在互联网上找到,但我得到的是这里。

Adding a custom header to HTTP request using angular.js

使用angular.js向HTTP请求添加自定义标头

So I followed the same, and changed the code to

所以我遵循相同的,并将代码更改为

Setting header

 var config = {headers:  {
                'Authorization': 'XXXYYY token="xxxxxxxx", realm="dash-api"',
                "X-Testing" : "testing"
                }
            };

The get request call:

获取请求调用:

return $http.get(api.host+'/agn/12/adv/1860/cam?status=1', config).then(function (response) {
                    return {
                        status:"success",
                        data:response.data.data.active
                    };
                }, function (error) {
                    return {
                        status:"error",
                        data:error
                    }
                });

As you can see the request are going in method type OPTIONS, and the Authorization token is not set in the request.

正如您所看到的,请求是在方法类型OPTIONS中进行的,并且请求中未设置授权令牌。

Please help me in this issue, as I am struggling from two days.

请帮我解决这个问题,因为我两天都在苦苦挣扎。

Thanks a lot.

非常感谢。

AngularJS  - 为GET请求设置HTTP标头

2 个解决方案

#1


1  

I think if you take a look at the link you refer to, you are getting the same issue as the author and the author of that post did state that it was a CORS issue - the server he was communicating with didn't support CORS, he confirms this in his comment on his own post.

我想如果你看一下你所指的链接,你会得到与作者相同的问题,该帖子的作者确实说这是一个CORS问题 - 他与之通信的服务器不支持CORS,他在自己的评论中证实了这一点。

As for why you are getting an OPTIONS request, this is because CORS makes a "pre-flight" request to the server to establish if CORS is going to be supported, before it will make the actual request.

至于为什么要获得OPTIONS请求,这是因为CORS向服务器发出“飞行前”请求,以确定是否支持CORS,然后才能发出实际请求。

#2


0  

You are seeing a OPTIONS request most probably because you are making a cross origin request (CORS). Firstly your server should support CORS (by responding to OPTIONS request with a Access-Control-Allow-Origin header set in the response).

您最有可能看到OPTIONS请求,因为您正在进行跨源请求(CORS)。首先,您的服务器应支持CORS(通过响应中设置的Access-Control-Allow-Origin标头响应OPTIONS请求)。

If you are making a authenticated cross origin request, you need to set withCredentials to true when making the request.

如果要进行经过身份验证的跨源请求,则需要在发出请求时将withCredentials设置为true。

$http.get(url, { withCredentials: true })

#1


1  

I think if you take a look at the link you refer to, you are getting the same issue as the author and the author of that post did state that it was a CORS issue - the server he was communicating with didn't support CORS, he confirms this in his comment on his own post.

我想如果你看一下你所指的链接,你会得到与作者相同的问题,该帖子的作者确实说这是一个CORS问题 - 他与之通信的服务器不支持CORS,他在自己的评论中证实了这一点。

As for why you are getting an OPTIONS request, this is because CORS makes a "pre-flight" request to the server to establish if CORS is going to be supported, before it will make the actual request.

至于为什么要获得OPTIONS请求,这是因为CORS向服务器发出“飞行前”请求,以确定是否支持CORS,然后才能发出实际请求。

#2


0  

You are seeing a OPTIONS request most probably because you are making a cross origin request (CORS). Firstly your server should support CORS (by responding to OPTIONS request with a Access-Control-Allow-Origin header set in the response).

您最有可能看到OPTIONS请求,因为您正在进行跨源请求(CORS)。首先,您的服务器应支持CORS(通过响应中设置的Access-Control-Allow-Origin标头响应OPTIONS请求)。

If you are making a authenticated cross origin request, you need to set withCredentials to true when making the request.

如果要进行经过身份验证的跨源请求,则需要在发出请求时将withCredentials设置为true。

$http.get(url, { withCredentials: true })