我没有使用设置请求标头的beforeSend方法,而是决定使用jQuery $.ajax()调用的headers设置选项.当然,我在这里访问了这个页面,http://api.jquery.com/jQuery.ajax/,但文档很少,我找不到任何方法来设置多个标题和在该页面或其他任何地方这样做的格式.
@tahir:那为什么这不起作用?
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 + '
最佳答案
它说:
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标头的代码:
$.ajax("relative/url/action.do",{
success: function(){
alert("success");
},error: function(jqXHR,textStatus,errorThrown){
alert(textStatus + ": " + jqXHR.responseText );
},headers: {Accept: "application/json"},type : "POST"
});