如何传递授权标头$ resource [英] How do I pass authorization headers $resource

查看:143
本文介绍了如何传递授权标头$ resource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找并努力寻找传递授权标头的方法。我的错误是405方法不允许预检的响应有无效的HTTP状态代码405.苦苦思索如何传递该持票人令牌。



这是我的工厂:



I'm looking and struggling to find a way to pass authorization headers. My error is 405 Method not allowed Response for preflight has invalid HTTP status code 405. Struggling on how to pass that bearer token.

Here is my factory:

angular.module("common.services").factory("employeeResource", [
    "$resource",
    "appSettings",
    "currentUser",
    employeeResource
]);

function employeeResource($resource, appSettings, currentUser) {
    return {
        getList: $resource(appSettings.serverPath + "api/employees ", null, {
            query: {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + currentUser.getProfile().token
                }
            }
        })
    };
}





用户个人资料



User Profiles

function currentUser() {
    var profile = {
        isLoggedIn: false,
        username: "",
        token: ""
    };
    var setProfile = function (username, token) {
        profile.username = username;
        profile.token = token;
        profile.isLoggedIn = true;
    };
    var getProfile = function () {
        return profile;
    };
    return {
        setProfile: setProfile,
        getProfile: getProfile
    }
}





我尝试过:



切换到$ http.get()

更改我的web.config和/或global.asax文件既不起作用



What I have tried:

Switching to $http.get()
Changing my web.config and or global.asax files neither worked

推荐答案

resource,
appSettings,
currentUser,
employeeResource
]);

函数employeeResource(
resource", "appSettings", "currentUser", employeeResource ]); function employeeResource(


resource,appSettings,currentUser){
return {
getList:
resource, appSettings, currentUser) { return { getList:


资源(appSettings.serverPath +api / employees,null,{
查询:{
方法:'获取',
标题:{
'授权':'持票人'+ currentUser.getProfile()。令牌
}
}
})
};
}
resource(appSettings.serverPath + "api/employees ", null, { query: { method: 'GET', headers: { 'Authorization': 'Bearer ' + currentUser.getProfile().token } } }) }; }





用户个人资料



User Profiles

function currentUser() {
    var profile = {
        isLoggedIn: false,
        username: "",
        token: ""
    };
    var setProfile = function (username, token) {
        profile.username = username;
        profile.token = token;
        profile.isLoggedIn = true;
    };
    var getProfile = function () {
        return profile;
    };
    return {
        setProfile: setProfile,
        getProfile: getProfile
    }
}





我尝试过:



切换到



What I have tried:

Switching to


这篇关于如何传递授权标头$ resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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