允许跨域ajax请求中的标头

use*_*531 4 apache ajax http cors

第一个Ajax请求接收响应,但不包括第二个包含标头的响应.如何允许标头包含在跨域Ajax请求中.

注意 - 我希望我需要进行服务器端更改,并且/sf/answers/2249830551/也这样说,但没有说明如何进行这些更改.此外,我不是在寻找仅限jQuery的解决方案.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Testing</title>  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
        <script type="text/javascript"> 
            $(function(){
                $.ajax({
                    type:'POST',
                    url:'http://otherdomain.example.com/ajax.php',
                })
                $.ajax({
                    type:'POST',
                    url:'http://otherdomain.example.com/ajax.php',
                    headers: {"X-Test-Header": "test-value"},
                })
            });
        </script>
    </head>

    <body></body> 
</html> 
Run Code Online (Sandbox Code Playgroud)

FF对第二个Ajax请求的响应:

跨源请求已阻止:同源策略不允许在http://otherdomain.example.com/ajax.php上读取远程资源.(原因:在CORS预检频道的CORS标题'Access-Control-Allow-Headers'中丢失了令牌'x-test-header').

Chrome响应第二个:

XMLHttpRequest无法加载http://otherdomain.example.com/ajax.php.请求标头字段在预检响应中,Access-Control-Allow-Header不允许使用X-Test-Header.Apache被设置为

如下:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization, Client-Security-Token, Accept-Encoding"
Run Code Online (Sandbox Code Playgroud)

use*_*531 6

似乎必须明确列出每个标头,我添加x-test-headerAccess-Control-Allow-Headers.

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "x-test-header, Origin, X-Requested-With, Content-Type, Accept"
Run Code Online (Sandbox Code Playgroud)