错误:Access-Control-Allow-Headers不允许Content-Type

时间:2011-02-17 10:28:35

标签: ajax cors

我在尝试发送ajax请求时在Chrome中收到此错误:

Content-Type is not allowed by Access-Control-Allow-Headers

Firefox中一切正常。

有人可以帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:100)

我解决了向Apache Web Server虚拟主机配置添加以下设置的问题

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"

答案 1 :(得分:10)

PHP解决方案:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

(需要在任何其他内容之前发送)

答案 2 :(得分:3)

在节点中设置CORS(跨站点HTTP请求)。对我来说,它看起来如下:

app.use('/api', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
  next();
});

答案 3 :(得分:2)

我遇到了同样的问题,我通过添加以下标题解决了这个问题: Access-Control-Allow-Headers:content-type

答案 4 :(得分:1)

for nginx

location / {
    proxy_pass http://localhost:59100;
    proxy_http_version 1.1;
    # proxy_set_header Upgrade $http_upgrade;
    # proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;

    # Simple requests
    if ($request_method ~* "(GET|POST)") {
      add_header "Access-Control-Allow-Origin"  *;
    }

    # Preflighted requests
    if ($request_method = OPTIONS ) {
      add_header "Access-Control-Allow-Origin"  *;
      add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
      add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
    }

    # proxy_cache_bypass $http_upgrade;
    # add_header Access-Control-Allow-Origin *;
    # add_header Access-Control-Allow-Headers Content-Type;
}

https://distinctplace.com/2017/04/17/nginx-access-control-allow-origin-cors/

答案 5 :(得分:0)

对我来说,即使我只设置了这个标题设置,localy也能正常工作:

import {ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy} from '@angular/router'; export class CustomReuseStrategy implements RouteReuseStrategy { handlers: { [key: string]: DetachedRouteHandle } = {}; shouldDetach(route: ActivatedRouteSnapshot): boolean { return true; } store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { this.handlers[this.getKey(route)] = handle; } shouldAttach(route: ActivatedRouteSnapshot): boolean { return !!route.routeConfig && !!this.handlers[this.getKey(route)]; } retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { if (!route.routeConfig) { return null; } return this.handlers[this.getKey(route)]; } shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { return curr.routeConfig === future.routeConfig; } private getKey(route: ActivatedRouteSnapshot) { let key: string; if (route.component) { key = route.component['name']; } else { key = route.firstChild.component['name']; } return key; } }