带有 $http 的 Access-Control-Allow-Headers 不允许请求标头字段 [英] Request header field is not allowed by Access-Control-Allow-Headers with $http
问题描述
我正在使用 Postman Chrome 扩展程序 对服务执行 POST,并得到预期的响应.
I'm doing a POST to a service using Postman Chrome Extension, and I get the expected response.
但是,当我使用 $http
执行相同的 POST 请求时,一切都会变得糟糕.
But, when I do the same POST request using $http
, all goes to hell.
我得到一个:
Request header field Engaged-Auth-Token is not allowed by Access-Control-Allow-Headers
Engaged-Auth-Token
作为标题.
我不知道为什么 Postman 可以工作,而 Chrome 却不能......
I've no idea why with Postman works and it doesn't work with Chrome...
有什么想法吗?
推荐答案
该问题是因为请求标头中缺少 Access-Control-Allow-Headers
.为了解决这个问题,我们需要添加 Access-Control-Allow-Headers: *
到请求头
The issue is because of missing Access-Control-Allow-Headers
from request Header. To fix this we need to add Access-Control-Allow-Headers: *
to request header
将 Access-Control-Allow-Headers
添加到 http 请求标头
.您可以使用 $httpProvider
在应用级别执行此操作.在您的应用配置部分添加以下行以添加此标头.
Add a Access-Control-Allow-Headers
to the http request header
. You can do this at app level using $httpProvider
. Add below line in your app config section to add this header.
var app = angular.module("app", [
"ngRoute",
"app.controllers",
"app.directives",
"app.filters"
]);
app.config([
"$routeProvider",
"$httpProvider",
function($routeProvider, $httpProvider){
$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
}
]);
这篇关于带有 $http 的 Access-Control-Allow-Headers 不允许请求标头字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!