ajax 错误:Access-Control-Allow-Headers 不允许内容类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5027705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Content-Type is not allowed by Access-Control-Allow-Headers
提问by Vinay Verma
I am getting this error in Chrome when trying to send an ajax request:
尝试发送 ajax 请求时,我在 Chrome 中收到此错误:
Content-Type is not allowed by Access-Control-Allow-Headers
Everything works fine in Firefox.
在 Firefox 中一切正常。
Can anyone help me to resolve this issue?
谁能帮我解决这个问题?
回答by Giulio Roggero
I solved the problem adding to Apache Web Server virtual host configuration the following settings
我解决了将以下设置添加到 Apache Web Server 虚拟主机配置的问题
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
回答by BlaM
Solution for PHP:
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)
(需要在任何其他内容之前发送)
回答by GentryRiggen
Set up CORS (Cross-site HTTP Requests) in node. For me it looks like the following:
在节点中设置 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();
});
回答by Van Coding
I had the same problem and I solved it by adding the following header: Access-Control-Allow-Headers: content-type
我遇到了同样的问题,我通过添加以下标题解决了它:Access-Control-Allow-Headers: content-type
回答by dcsan
for nginx
对于 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/
见 https://distinctplace.com/2017/04/17/nginx-access-control-allow-origin-cors/
回答by Pauls Bebris
To me with PHP, localy works even if i set only this header setting:
对我来说,使用 PHP,即使我只设置了这个标头设置,localy 也能工作:
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

