javascript 如何在Angular js中的http请求中发送标头数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20326514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:14:50  来源:igfitidea点击:

How to send header data in http request in Angular js

javascripthtmlajaxangularjs

提问by

While trying to access a secured Api, I need to send header data with in my angular http requests

在尝试访问安全的 Api 时,我需要在我的 angular http 请求中发送标头数据

javascript code :

javascript代码:

$http.post('http://localhost/api/validate', user).success(function () {
        $scope.reset();
        $scope.activePath = $location.path('/');

How to send header data with in this request?

如何在此请求中发送标头数据?

回答by

   //store the header data in a variable 
    var headers = { 'Authorization': authToken };

    //Add headers with in your request
    $http.post('http://localhost/api/validate',user, { headers: headers } ).success(function() 

回答by bagonyi

Setting HTTP Headers

设置 HTTP 标头

The $http service will automatically add certain HTTP headers to all requests. These defaults can be fully configured by accessing the $httpProvider.defaults.headers configuration object, which currently contains this default configuration:

$http 服务会自动向所有请求添加某些 HTTP 标头。这些默认值可以通过访问 $httpProvider.defaults.headers 配置对象来完全配置,该对象当前包含此默认配置:

$httpProvider.defaults.headers.common (headers that are common for all requests): Accept: application/json, text/plain, * / *

$httpProvider.defaults.headers.common(所有请求通用的headers):接受:application/json, text/plain, * /*

$httpProvider.defaults.headers.post: (header defaults for POST requests) Content-Type: application/json

$httpProvider.defaults.headers.post: (POST 请求的标头默认值) Content-Type: application/json

$httpProvider.defaults.headers.put (header defaults for PUT requests) Content-Type: application/json

$httpProvider.defaults.headers.put(PUT 请求的标头默认值)内容类型:application/json

To add or overwrite these defaults, simply add or remove a property from these configuration objects. To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. `$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }.

要添加或覆盖这些默认值,只需从这些配置对象中添加或删除属性。要为 POST 或 PUT 以外的 HTTP 方法添加标头,只需添加一个以小写 HTTP 方法名称作为键的新对象,例如 `$httpProvider.defaults.headers.get = { 'My-Header' : 'value' } .

The defaults can also be set at runtime via the $http.defaults object in the same fashion. In addition, you can supply a headers property in the config object passed when calling $http(config), which overrides the defaults without changing them globally.

默认值也可以在运行时通过 $http.defaults 对象以相同的方式设置。此外,您可以在调用 $http(config) 时传递的 config 对象中提供 headers 属性,它会覆盖默认值而不会全局更改它们。

回答by vinesh

For Authentication, I find this piece of code helpful. Assuming that the token is stored in cookie after successful login,

对于身份验证,我发现这段代码很有帮助。假设登录成功后token保存在cookie中,

.factory('authInterceptor', function ($q, $cookieStore) {
    return {
        // Add authorization token to headers
        request: function (config) {
            config.headers = config.headers || {};
            if ($cookieStore.get('token')) {
                config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
            }
            return config;
        },

        // Intercept 401s and redirect you to login
        responseError: function(response) {
            if(response.status === 401) {
                // remove any stale tokens
                $cookieStore.remove('token');
                return $q.reject(response);
            }
            else {
                return $q.reject(response);
            }
        }
    };
})
.config(function ($httpProvider) {
    $httpProvider.interceptors.push('authInterceptor');
})