本文翻译自:Add header in AJAX request with jQuery
I would like to add a custom header to an AJAX POST request from jQuery. 我想向jQuery的AJAX POST请求添加自定义标头。
I have tried this: 我已经试过了:
$.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);
});
When I send this request and I watch with FireBug, I see this header: 当我发送此请求并观看FireBug时,看到以下标头:
OPTIONS xxxx/yyyy HTTP/1.1 选项xxxx / yyyy HTTP / 1.1
Host: 127.0.0.1:6666 主持人:127.0.0.1:6666
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0 用户代理:Mozilla / 5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko / 20100101 Firefox / 11.0
Accept: text/html,application/xhtml+