asp.net - 如何在 angularJs 中添加 WebApi 标头
问题描述
我有以下代码可以从 localhost 调用 WebApi,效果很好。
var app = angular.module('myApp', []);
app.controller('employeesController', function ($scope, $http) {
$http({
Method: 'GET',
url: 'http://localhost:50515/api/employees'
}).then(function (result) {
$scope.Getemployees = result.data;
});
});
我还设置了 WebApi 服务,该服务使用邮递员进行了很好的测试。但我想包括 WebApi 标头信息:ApiKey 名称和 apikey 值。我怎样才能修改上面的脚本?
url: https://example.api.services/api/XX/v2/employees/all //url from postman
Key: api-key
value: ba741c13417b7775524dbf0f417cefc4
解决方案
在headers
参数中添加一个带有键值对的对象。
$http({
Method: 'GET',
url: 'http://localhost:50515/api/employees',
headers: {
'api-key': 'ba741c13417b7775524dbf0f417cefc4'
},
}).then(function (result) {
$scope.Getemployees = result.data;
});