Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers

后端 未结 15 1274
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 16:31

I\'m trying to send files to my server with a post request, but when it sends it causes the error:

Request header field Content-Type is not allowed by

相关标签:
15条回答
  • 2020-11-22 16:32

    The headers you are trying to set are response headers. They have to be provided, in the response, by the server you are making the request to.

    They have no place being set on the client. It would be pointless having a means to grant permissions if they could be granted by the site that wanted permission instead of the site that owned the data.

    0 讨论(0)
  • 2020-11-22 16:35

    If that helps anyone, (even if this is kind of poor as we must only allow this for dev purpose) here is a Java solution as I encountered the same issue. [Edit] Do not use the wild card * as it is a bad solution, use localhost if you really need to have something working locally.

    public class SimpleCORSFilter implements Filter {
    
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "my-authorized-proxy-or-domain");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        chain.doFilter(req, res);
    }
    
    public void init(FilterConfig filterConfig) {}
    
    public void destroy() {}
    
    }
    
    0 讨论(0)
  • 2020-11-22 16:36

    The following works for me with nodejs:

    xServer.use(function(req, res, next) {
      res.setHeader("Access-Control-Allow-Origin", 'http://localhost:8080');
      res.setHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS,PUT,DELETE');
      res.setHeader('Access-Control-Allow-Headers', 'Content-Type,Accept');
    
      next();
    });
    
    0 讨论(0)
  • 2020-11-22 16:38

    The server (that the POST request is sent to) needs to include the Content-Type header in its response.

    Here's a list of typical headers to include, including one custom "X_ACCESS_TOKEN" header:

    "X-ACCESS_TOKEN", "Access-Control-Allow-Origin", "Authorization", "Origin", "x-requested-with", "Content-Type", "Content-Range", "Content-Disposition", "Content-Description"
    

    That's what your http server guy needs to configure for the web server that you're sending your requests to.

    You may also want to ask your server guy to expose the "Content-Length" header.

    He'll recognize this as a Cross-Origin Resource Sharing (CORS) request and should understand the implications of making those server configurations.

    For details see:

    • http://www.w3.org/TR/cors/
    • http://enable-cors.org/
    0 讨论(0)
  • 2020-11-22 16:38

    You can activate the proper header in PHP with this:

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");
    
    0 讨论(0)
  • 2020-11-22 16:39

    I had the same problem. In the jQuery documentation I found:

    For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

    So though the server allows cross origin request but does not allow Access-Control-Allow-Headers , it will throw errors. By default angular content type is application/json, which is trying to send a OPTION request. Try to overwrite angular default header or allow Access-Control-Allow-Headers in server end. Here is an angular sample:

    $http.post(url, data, {
        headers : {
            'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    });
    
    0 讨论(0)
提交回复
热议问题