javascript 使用 $http 在 AngularJS 中调用 cURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26769583/
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-28 06:27:50  来源:igfitidea点击:

cURL call in AngularJS using $http

javascriptangularjscurl

提问by bobcobb

I am new to angular and I'm trying to pass an access token in my header, but I cant seem to get it right.

我是 angular 的新手,我试图在我的标头中传递一个访问令牌,但我似乎无法做到正确。

I have a curl request that works fine and I'm trying to get it in working in angular:

我有一个运行良好的 curl 请求,我正在尝试让它在 angular 中工作:

curl http://localhost:3000/api/v1/users -IH "Authorization: Token api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Angular $http call that isn't working

无效的 Angular $http 调用

$http.get('http://localhost:3000/api/v1/users', {headers: {'api_key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'}}).then(function(response) {
                service.currentUser = response.data.user;
                console.log(service.currentUser);

Thanks!

谢谢!

回答by rtucker88

Based on your curl I think the request should look like:

根据您的卷曲,我认为请求应如下所示:

$http.get('http://localhost:3000/api/v1/users', 
    {headers: { Authorization: ' Token api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxx'}})
    .then(function(response) {
            service.currentUser = response.data.user;
            console.log(service.currentUser);
    });