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

发布于 2024-10-17 17:26:10 字数 170 浏览 5 评论 0原文

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

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

在 Firefox 中一切正常。

谁能帮我解决这个问题?

I am getting this error in Chrome when trying to send an ajax request:

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

Everything works fine in Firefox.

Can anyone help me to resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最美不过初阳 2024-10-24 17:26:10

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

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

I solved the problem adding to Apache Web Server virtual host configuration the following settings

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
嘿看小鸭子会跑 2024-10-24 17:26: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');

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

Solution for 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');

(Need to send that before any other content)

寂寞笑我太脆弱 2024-10-24 17:26:10

在node.js中设置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();
});

Set up CORS (Cross-site HTTP Requests) in node. For me it looks like the following:

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();
});
孤独岁月 2024-10-24 17:26:10

对于 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/

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;
}

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

寒尘 2024-10-24 17:26:10

我遇到了同样的问题,我通过添加以下标头解决了它:
访问控制允许标头:内容类型

I had the same problem and I solved it by adding the following header:
Access-Control-Allow-Headers: content-type

清欢 2024-10-24 17:26:10

对于使用 PHP 的我来说,即使我仅设置此标头设置,本地也可以工作:

header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

To me with PHP, localy works even if i set only this header setting:

header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文