【问题标题】:Error: Content-Type is not allowed by Access-Control-Allow-Headers错误:Access-Control-Allow-Headers 不允许 Content-Type
【发布时间】:2011-06-29 00:01:43
【问题描述】:

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

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

在 Firefox 中一切正常。

谁能帮我解决这个问题?

【问题讨论】:

    标签: ajax cors


    【解决方案1】:

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

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

    【讨论】:

    • 我遇到了同样的问题。如果您使用的是 Django,请使用 CORS HEADERS 模块,检查这个 CORS_ALLOW_HEADERS - github.com/ottoyiu/django-cors-headers
    • @GiulioRoggero :我知道我不应该在这里问这个问题,但是在将标题添加到响应之后,我遇到了同样的问题。
    • 请问我应该在哪个文件中添加这两行?我已经在 .htaccess 文件 og Magento 中设置了这个,但我仍然收到预检错误消息!!
    • @SachinS 这是 apache.conf 文件
    【解决方案2】:

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

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

    【讨论】:

    • 这个信息是正确的,下面是我的解决方案的“完整”代码。 header('Access-Control-Allow-Origin: http://example.com'); header('Access-Control-Allow-Methods: GET,OPTIONS'); header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); echo $_GET['callback'].json_encode($data);
    【解决方案3】:

    在 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();
    });
    

    【讨论】:

    • 您能否建议如何在 Angular 2 中解决此问题:在关注了我仍然面临的所有帖子之后:预检响应具有无效的 HTTP 状态代码 400
    【解决方案4】:

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

    【讨论】:

    • 您将此标头添加到什么位置?对于您在客户端发起的请求?
    • 不能代表@Van Coding,但要使其工作,您应该将此标头添加到http响应中。
    【解决方案5】:

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

    【讨论】:

      【解决方案6】:

      对 PHP 来说,即使我只设置了这个标头设置,localy 也可以工作:

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

      【讨论】:

        猜你喜欢
        • 2012-09-06
        • 2015-06-17
        • 2017-02-19
        • 2013-07-05
        • 2016-11-24
        • 2014-06-16
        • 2015-06-03
        相关资源
        最近更新 更多