最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send header data in http request in Angular js - Stack Overflow

programmeradmin3浏览0评论

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

javascript code :

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

How to send header data with in this request?

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

javascript code :

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

How to send header data with in this request?

Share Improve this question asked Dec 2, 2013 at 10:54 user2853731user2853731
Add a ment  | 

3 Answers 3

Reset to default 11
   //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() 

Setting HTTP Headers

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:

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

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

$httpProvider.defaults.headers.put (header defaults for PUT requests) Content-Type: 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' }.

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.

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

.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');
})
发布评论

评论列表(0)

  1. 暂无评论