使用 angular.js 向 HTTP 请求添加自定义标头 [英] Adding a custom header to HTTP request using angular.js

查看:47
本文介绍了使用 angular.js 向 HTTP 请求添加自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angular.js 的新手,我正在尝试向请求添加一些标头:

I am a novice to angular.js, and I am trying to add some headers to a request:

   var config = {headers: {
            'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
            'Accept': 'application/json;odata=verbose'
        }
    };

   $http.get('https://www.example.com/ApplicationData.svc/Malls(1)/Retailers', config).success(successCallback).error(errorCallback);

我查看了所有文档,在我看来这应该是正确的.

I've looked at all the documentation, and this seems to me like it should be correct.

当我为 $http.get 中的 URL 使用本地文件时,我在 Chrome 的网络选项卡上看到以下 HTTP 请求:

When I use a local file for the URL in the $http.get, I see the following HTTP request on the network tab in Chrome:

GET /app/data/offers.json HTTP/1.1
Host: www.example.com
Connection: keep-alive
Cache-Control: max-age=0
If-None-Match: "0f0abc9026855b5938797878a03e6889"
Authorization: Basic Y2hhZHN0b25lbWFuOkNoYW5nZV9tZQ==
Accept: application/json;odata=verbose
X-Requested-With: XMLHttpRequest
If-Modified-Since: Sun, 24 Mar 2013 15:58:55 GMT
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
X-Testing: Testing
Referer: http://www.example.com/app/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

如您所见,两个标题均已正确添加.但是当我将 URL 更改为上面 $http.get 中显示的 URL 时(除了使用真实地址,而不是 example.com),然后我得到:

As you can see, both of the headers were added correctly. But when I change the URL to the one shown in the $http.get above (except using the real address, not example.com), then I get:

OPTIONS /ApplicationData.svc/Malls(1) HTTP/1.1
Host: www.datahost.net
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://mpon.site44.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Access-Control-Request-Headers: accept, origin, x-requested-with, authorization, x-testing
Accept: */*
Referer: http://mpon.site44.com/app/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

这两者在代码上的唯一区别是第一个 URL 是本地文件,第二个 URL 是远程服务器.如果您查看第二个请求标头,则没有 Authentication 标头,并且 Accept 似乎使用默认值而不是指定的值.此外,第一行现在说 OPTIONS 而不是 GET(虽然 Access-Control-Request-MethodGET).

The only difference in code between these two is one is for the first the URL is a local file, and for the second the URL is a remote server. If you look at the second Request header, there is no Authentication header, and the Accept appears to be using a default instead of the one specified. Also, the first line now says OPTIONS instead of GET (although Access-Control-Request-Method is GET).

知道上面的代码有什么问题吗,或者在不使用本地文件作为数据源时如何获取包含的附加标头?

Any idea what is wrong with the above code, or how to get the additional headers included using when not using a local file as a data source?

推荐答案

我拿走了你的东西,并添加了另一个 X-Testing 标题

I took what you had, and added another X-Testing header

var config = {headers:  {
        'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
        'Accept': 'application/json;odata=verbose',
        "X-Testing" : "testing"
    }
};

$http.get("/test", config);

在 Chrome 网络标签中,我看到他们正在发送.

And in the Chrome network tab, I see them being sent.

GET /test HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: application/json;odata=verbose
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Authorization: Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==
X-Testing: testing
Referer: http://localhost:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

您是不是从浏览器或服务器上看到它们?尝试使用浏览器工具或调试代理,看看发送的是什么.

Are you not seeing them from the browser, or on the server? Try the browser tooling or a debug proxy and see what is being sent out.

这篇关于使用 angular.js 向 HTTP 请求添加自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆