角$资源transformRequest不会改变请求头 [英] Angular $resource transformRequest doesn't change Request Header

查看:1254
本文介绍了角$资源transformRequest不会改变请求头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用$资源的REST登录,按照预期的transformRequest不会添加Authorization头。使用$就调用按预期它的工作。
因此,使用:

When I use $resource for a REST login, the transformRequest doesn't add the Authorization header as intended. Using a $.ajax call it does work as intended. So using:

    $scope.login2 = function() {
    function setHeader(xhr){xhr.setRequestHeader("Authorization", "Basic " + btoa($scope.gebruikersnaam + ':' + $scope.wachtwoord))}
    $.ajax({type: "POST",  url: "http://localhost:8000/authview/",  beforeSend: setHeader}).
        fail(function(resp){
            console.log('bad credentials.')
        }).
        done(function(resp){
            console.log('welcome ' + resp.email)
        })
}

我得到添加到请求授权头:

I get the authorization header added to the request:

Origin: http://localhost
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36   (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Authorization: Basic YWRtaW46cGFzc3dvcmQ=

但这样做时:

    $scope.login = function() {
  api.auth.login($scope.getCredentials()).
             $promise.
                 then(function(data){
                     // on good username and password
                     $scope.gebruikersnaam = data.username;
                 }).
                 catch(function(data){
                     // on incorrect username and password
                     alert(data.data.detail);
                 });      
};

,其中api.auth.login的定义是这样的:

where "api.auth.login" is defined like:

kmregistratieApp.factory('api', function($resource){
    function add_auth_header(data, headersGetter){
        var headers = headersGetter();
        headers['Authorization'] = ('Basic ' + btoa(data.username + ':' + data.password));
    }
    return {
        auth: $resource('http://localhost:8000/authview/', {}, {
            login: {method: 'POST', transformRequest: add_auth_header},
            logout: {method: 'DELETE'}
        }),
        users: $resource('http://localhost:8000/authview/', {}, {
            create: {method: 'POST'}
        })
    };
});

在标题['授权'] =(基本+ ...(调试时),我可以看到它坐在headersGetter:

After "headers['Authorization'] = ('Basic ' + ..." (when debugging) I can see it sitting in headersGetter:

headers: Object
Authorization: "Basic YWRtaW46cGFzc3dvcmQ="
accept: "application/json, text/plain, */*"
content-type: "application/json;charset=utf-8"

但检查头时,它不会在网络选项卡打开了。
所以我的问题是为什么不干活不添加授权头的$资源的方式?

But it doesn't turn up in the Network tab when inspecting the headers. So my question is why doesn't the $resource way of working not add the Authorization header?

推荐答案

并不意味着TransformRequest被用于修改标头。
见<一href=\"https://github.com/angular/angular.js/blob/master/CHANGELOG.md#user-content-breaking-changes-9\"相对=nofollow> AngularJS的changelog 。向下滚动一点,你会看到这一点:

TransformRequest is not meant to be used to modify headers. See AngularJS changelog. Scroll a bit downwards and you will see this:

transformRequest功能,可以再修改请求头。

transformRequest functions can no longer modify request headers.

HTTP使用的$ HTTP头文件时才能指定。例如:

HTTP headers can only be specified when using $http. Example:

$http.post('/someUrl', data, { headers: { 'Authorization': 'Basic'+key } });

这篇关于角$资源transformRequest不会改变请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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