【发布时间】:2017-02-02 06:08:10
【问题描述】:
第一个 Ajax 请求收到响应,但第二个没有包含标头的响应。如何允许标头包含在跨域 Ajax 请求中。
注意 - 我希望我需要进行服务器端更改,https://stackoverflow.com/a/32140436/1032531 也这么说,但没有说明如何进行这些更改。另外,我不是在寻找仅 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>
FF 响应第二个 Ajax 请求:
跨域请求被阻止:同源策略不允许读取 http://otherdomain.example.com/ajax.php 的远程资源。 (原因:CORS 标头中缺少标记“x-test-header” 来自 CORS 预检通道的“Access-Control-Allow-Headers”)。
Chrome 将第二个响应为:
XMLHttpRequest 无法加载 http://otherdomain.example.com/ajax.php。 请求头字段 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"
【问题讨论】: