gpt4 book ai didi

javascript - 如何在 Angular JS 中使用 JWT 调用 REST API

转载 作者:行者123 更新时间:2023-12-02 23:27:37 26 4
gpt4 key购买 nike

我在 webapi 中创建了一个带有基于 jwt token 的身份验证的 Web API。我想用 Angular js $http 调用 html 页面中的其余 api。我尝试过,但显示请求失败。我从 postman 工具获得响应,但在网页中。不知道如何在 header 中传递不记名 token 。

postman 请求

GET /api/mp/dashboard HTTP/1.1
Host: localhost:55417
Authorization: Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiQWRtaW4iLCJleHAiOjE1NjA5NDcyMzMsImlzcyI6Im15c2l0ZS5jb20iLCJhdWQiOiJteXNpdGUuY29tIn0.SMTy2H5vmWWUgCytHEHfT847ipE2hCzk0wvP2Of60Uk

Angular js代码

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http.get("http://ip_address:3393/api/mp/dashboard", {
headers: { 'Authorization': 'Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiQWRtaW4iLCJleHAiOjE1NjA5NDcyMzMsImlzcyI6Im15c2l0ZS5jb20iLCJhdWQiOiJteXNpdGUuY29tIn0.SMTy2H5vmWWUgCytHEHfT847ipE2hCzk0wvP2Of60Uk' }
})
.then(function (response) {
$scope.myWelcome = response.data;
$scope.fridgetemp = $scope.myWelcome.fridge_temp;
});
});

最佳答案

尝试此 Http get 调用,对于 header ,您必须在名为 Content-Type : 'application/json' 的对象中传递一个字段以及授权

$http({
method: "GET",
url: 'http://ip_address:3393/api/mp/dashboard',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiQWRtaW4iLCJleHAiOjE1NjA5NDcyMzMsImlzcyI6Im15c2l0ZS5jb20iLCJhdWQiOiJteXNpdGUuY29tIn0.SMTy2H5vmWWUgCytHEHfT847ipE2hCzk0wvP2Of60Uk'
},
data: '' //this field depends on coder.
})
.then(function (response) {
//your success result code handling
},
function (error) {
//your error handling code
}
});

关于javascript - 如何在 Angular JS 中使用 JWT 调用 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56667979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com