我想从jQuery向AJAX POST请求添加自定义标头。
我试过这个:
$.ajax({
type: 'POST',
url: url,
headers: {
"My-First-Header":"first value",
"My-Second-Header":"second value"
}
//OR
//beforeSend: function(xhr) {
// xhr.setRequestHeader("My-First-Header", "first value");
// xhr.setRequestHeader("My-Second-Header", "second value");
//}
}).done(function(data) {
alert(data);
});
当我发送此请求并使用FireBug观看时,我看到此标题:
选项xxxx/yyyy HTTP/1.1
主持人:127.0.0.1:6666
User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko/20100101 Firefox/11.0
接受:text/html,application/xhtml + xml,application/xml; q = 0.9, / ; q = 0.8
接受语言:fr,fr-fr; q = 0.8,en-us; q = 0.5,en; q = 0.3
接受编码:gzip,deflate
连接:保持活力
原产地:无效
访问控制请求方法:POST
Access-Control-Request-Headers:my-first-header,my-second-header
Pragma:no-cache
缓存控制:无缓存
为什么我的自定义标题转到Access-Control-Request-Headers
:
Access-Control-Request-Headers:my-first-header,my-second-header
我期待像这样的标头值:
My-First-Header:第一个值
My-Second-Header:第二个值
可能吗?
你在Firefox中看到的不是实际的要求;请注意,HTTP方法是OPTIONS,而不是POST。这实际上是浏览器确定是否应允许跨域AJAX请求的“飞行前”请求:
飞行前请求中的Access-Control-Request-Headers标头包括实际请求中的标头列表。然后,在浏览器提交实际请求之前,服务器将报告是否在此上下文中支持这些标头。
以下是如何在JQuery Ajax调用中设置请求标头的示例:
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("Authority", authorizationToken);
},
url: "entities",
data: "json=" + escape(JSON.stringify(createRequestObject)),
processData: false,
success: function(msg) {
$("#results").append("The result =" + StringifyPretty(msg));
}
});
以下代码适用于我。我总是只使用单引号,它工作正常。我建议你应该只使用单引号OR只用双引号,但不能混淆。
$.ajax({
url: 'YourRestEndPoint',
headers: {
'Authorization':'Basic xxxxxxxxxxxxx',
'X-CSRF-TOKEN':'xxxxxxxxxxxxxxxxxxxx',
'Content-Type':'application/json'
},
method: 'POST',
dataType: 'json',
data: YourData,
success: function(data){
console.log('succes: '+data);
}
});
希望这能回答你的问题......
这就是为什么你不能用Javascript创建机器人,因为你的选择仅限于浏览器允许你做的事情。您不能只按照大多数浏览器遵循的CORS
政策订购浏览器,将随机请求发送到其他来源,并让您轻松获得响应!
此外,如果您尝试从浏览器附带的开发人员工具手动编辑某些请求标头(如Origin-header
),浏览器将拒绝您的编辑,并可能会发送预检OPTIONS
请求。
因为你发送自定义标题所以你的CORS请求是 不是简单的请求 所以浏览器首先发送prefilight OPTIONS请求来检查服务器是否允许你的请求。
如果您在服务器上打开CORS,那么您的代码将起作用。您也可以使用js fetch( 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');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
这是在nginx上打开 CORS的示例配置 (nginx.conf文件)
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;
}
}
这是在Apache上打开 CORS的示例配置 (。htaccess文件)
# ------------------------------------------------------------------------------
# | Cross-domain AJAX requests |
# ------------------------------------------------------------------------------
# Enable cross-Origin AJAX requests.
# http://code.google.com/p/html5security/wiki/CrossOriginRequestSecurity
# http://enable-cors.org/
# <IfModule mod_headers.c>
# Header set Access-Control-Allow-Origin "*"
# </IfModule>
#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"