gpt4 book ai didi

ajax - 允许跨域 ajax 请求中的 header

转载 作者:可可西里 更新时间:2023-11-01 15:27:00 25 4
gpt4 key购买 nike

第一个 Ajax 请求会收到响应,但第二个请求不会收到包含 header 的响应。如何允许 header 包含在跨域 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 请求的响应:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://otherdomain.example.com/ajax.php. (Reason: missing token 'x-test-header' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).

Chrome 响应第二个:

XMLHttpRequest cannot load http://otherdomain.example.com/ajax.php. Request header field X-Test-Header is not allowed by Access-Control-Allow-Headers in preflight response. Apache is set up as

如下:

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"

最佳答案

似乎每个 header 都必须明确列出,我将 x-test-header 添加到 Access-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"

关于ajax - 允许跨域 ajax 请求中的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39676722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com