[英]Request header field is not allowed by Access-Control-Allow-Headers with $http
I'm doing a POST to a service using Postman Chrome Extension , and I get the expected response. 我正在使用Postman Chrome Extension对服务进行POST ,我得到了预期的响应。
But, when I do the same POST request using $http
, all goes to hell. 但是,当我使用
$http
执行相同的POST请求时,一切都会变成地狱。
I get a : 我得到一个:
Request header field Engaged-Auth-Token is not allowed by Access-Control-Allow-Headers
Engaged-Auth-Token
being a header. Engaged-Auth-Token
是标题。
I've no idea why with Postman works and it doesn't work with Chrome... 我不知道为什么Postman有效并且它不适用于Chrome ...
Any ideas? 有任何想法吗?
I believe configuring the Access-Control-Allow-Headers on the $httpProvider on the CLIENT will not work. 我相信在CLIENT上的$ httpProvider上配置Access-Control-Allow-Headers将不起作用。 I think the header needs to be configured on the server (as a response header).
我认为需要在服务器上配置标头(作为响应标头)。 In a node-express application for instance, this could be done with a middleware (for example), putting something like this:
例如,在一个节点表达式应用程序中,这可以通过中间件(例如)完成,如下所示:
res.header('*')
or (more selectively) just the headers you need: 或者(更有选择性地)只需要你需要的标题:
res.header('Engaged-Auth-Token, Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
The issue is because of missing Access-Control-Allow-Headers
from request Header. 问题是因为请求标头中缺少
Access-Control-Allow-Headers
。 To fix this we need to add Access-Control-Allow-Headers: *
to request header 要解决此问题,我们需要添加
Access-Control-Allow-Headers: *
来请求标头
Add a Access-Control-Allow-Headers
to the http request header
. 将
Access-Control-Allow-Headers
到http request header
。 You can do this at app level using $httpProvider
. 您可以使用
$httpProvider
在应用程序级别执行此$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'] = '*';
}
]);
if use sails api on backend change cors.js and add your token filed here 如果在后端使用sails api更改cors.js并在此处添加您的令牌
module.exports.cors = {
allRoutes: true,
origin: '*',
credentials: true,
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',
headers: 'Origin, X-Requested-With, Content-Type, Accept, Engaged-Auth-Token'
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.