Access Control Request Headers, is added to header in AJAX request with jQuery

前端 未结 6 1407
不知归路
不知归路 2020-11-22 10:29

I would like to add a custom header to an AJAX POST request from jQuery.

I have tried this:

$.ajax({
    type: \'POST\',
    url: url,
    headers:          


        
6条回答
  •  孤城傲影
    2020-11-22 11:00

    Because you send custom headers so your CORS request is not a simple request, so the browser first sends a preflight OPTIONS request to check that the server allows your request.

    If you turn on CORS on the server then your code will work. You can also use JavaScript fetch instead (here)

    let url='https://server.test-cors.org/server?enable=true&status=200&methods=POST&headers=My-First-Header,My-Second-Header';
    
    
    $.ajax({
        type: 'POST',
        url: url,
        headers: {
            "My-First-Header":"first value",
            "My-Second-Header":"second value"
        }
    }).done(function(data) {
        alert(data[0].request.httpMethod + ' was send - open chrome console> network to see it');
    });

    Here is an example configuration which turns on CORS on nginx (nginx.conf file):

    location ~ ^/index\.php(/|$) {
       ...
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' "$http_origin"; # DO NOT remove THIS LINES (doubled with outside 'if' above)
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Max-Age' 1728000; # cache preflight value for 20 days
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'My-First-Header,My-Second-Header,Authorization,Content-Type,Accept,Origin';
            add_header 'Content-Length' 0;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            return 204;
        }
    }

    Here is an example configuration which turns on CORS on Apache (.htaccess file)

    # ------------------------------------------------------------------------------
    # | Cross-domain Ajax requests                                                 |
    # ------------------------------------------------------------------------------
    
    # Enable cross-origin Ajax requests.
    # http://code.google.com/p/html5security/wiki/CrossOriginRequestSecurity
    # http://enable-cors.org/
    
    # 
    #    Header set Access-Control-Allow-Origin "*"
    # 
    
    #Header set Access-Control-Allow-Origin "http://example.com:3000"
    #Header always set Access-Control-Allow-Credentials "true"
    
    Header set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
    Header always set Access-Control-Allow-Headers "My-First-Header,My-Second-Header,Authorization, content-type, csrf-token"

提交回复
热议问题