gpt4 book ai didi

jquery - 在哪里可以找到合适的 jQuery ajax 标题设置文档?

转载 作者:行者123 更新时间:2023-12-03 23:06:13 25 4
gpt4 key购买 nike

我决定考虑使用 jQuery $.ajax() 调用的 headers 设置选项,而不是使用 beforeSend 方法设置请求 header 。自然地,我来到了这个页面,http://api.jquery.com/jQuery.ajax/ ,但文档很少,我找不到任何方法来设置多个标题以及在该页面或其他任何地方设置多个标题的格式。

@tahir:那为什么这不起作用?

<!DOCTYPE html>
<html>
<head>
<title>Multiple DnD Uploader</title>
<link rel="stylesheet" href="style.css" />
<script type = "text/javascript" src = "../music/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#drop').change(function(event){
files = event.target.files;
$('#drop').css('display', 'none');
for(var i = 0, len = files.length; i < len; i++) {
alert(files[i].fileName);
$.ajax({
type: "POST",
url: "uploader.php",
contentType: "multipart/form-data",
headers: {
"X-File-Name" : ""+files[i].fileName,
"X-File-Size" : ""+files[i].fileSize
},
data: 'hi',
success: function(data){
$('#info').append('Success: ' + data + '<br />');
},error: function(data){
$('#info').append('Error: ' + data + '<br />');
}
});
}
});
});
</script>
</head>
<body>
<div id="drop">
<h1>Drop files here</h1>
<p>To add them as attachments</p>
<input type="file" multiple="true" id="filesUpload" />
</div>
<div id="info">
</div>
</body>
</html>

两个 X-File-Name 和 X-File-Size 属性未显示在请求 header 中。

解决方案:我觉得真的很愚蠢,事实证明我指向的特定 jquery.js 文件是 1.4.4,所以我升级了,现在它可以工作了!谢谢。

最佳答案

它说:

A map of additional header key/value pairs to send along with the request. This setting is set before the beforeSend function is called; therefore, any values in the headers setting can be overwritten from within the beforeSend function.

所以你所要做的就是传递一个像这样的对象:

{"header1":"value1","header2":"value2"}

等等。以下是在 post 请求中添加 Accept header 的一些代码:

    $.ajax("relative/url/action.do",{
success: function(){
alert("success");
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus + ": " + jqXHR.responseText );
},
headers: {Accept: "application/json"},
type : "POST"
});

关于jquery - 在哪里可以找到合适的 jQuery ajax 标题设置文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6078425/

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