简体   繁体   English

具有$ http的Access-Control-Allow-Header不允许使用请求标头字段

[英]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-Headershttp 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.

相关问题 Access-Control-Allow-Headers 不允许请求头字段 Access-Control-Allow-Headers - Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers $ http.post-请求标头字段Access-Control-Allow-Headers不允许授权 - $http.post - Request header field Authorization is not allowed by Access-Control-Allow-Headers $ http.post请求标头字段Access-Control-Allow-Headers错误不允许使用Access-Control-Allow-Origin - $http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers error 预检响应中的 Access-Control-Allow-Headers 不允许 Angularjs 请求标头字段 Access-Control-Allow-Headers - Angularjs Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers in preflight response 请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin - Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response angularJS $ http.Post无法正常工作:无法加载资源:Access-Control-Allow-Headers不允许请求标头字段0 - angularJS $http.Post not working: Failed to load resource: Request header field 0 is not allowed by Access-Control-Allow-Headers Access-Control-Allow-Headers不允许请求标头字段x-access-token - Request header field x-access-token is not allowed by Access-Control-Allow-Headers ajax请求标头字段Content-Type被Access-Control-Allow-Headers不允许 - ajax Request header field Content-Type is not allowed by Access-Control-Allow-Headers 离子:在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段令牌 - Ionic: Request header field token is not allowed by Access-Control-Allow-Headers in preflight response Grunt Request标头字段Access-Control-Allow-Headers不允许授权 - Grunt Request header field Authorization is not allowed by Access-Control-Allow-Headers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM