我在哪里可以找到体面的jQuery ajax标题设置文档?

时间:2022-08-08 00:03:33

Instead of using the beforeSend method of setting the request headers, I decided I wanted to look into using the headers setting option of a jQuery $.ajax() call. Naturally, I went to this page here, http://api.jquery.com/jQuery.ajax/, but the documentation is scarce and I can't find any way to set multiple headers and the format for doing so on that page, or anywhere else.

我没有使用设置请求标头的beforeSend方法,而是决定使用jQuery $ .ajax()调用的headers设置选项。当然,我在这里访问了这个页面,http://api.jquery.com/jQuery.ajax/,但文档很少,我找不到任何方法来设置多个标题以及在该页面上执行此操作的格式或其他任何地方。

@tahir: Then why is this not working?

@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>

The two X-File-Name and X-File-Size attributes aren't showing up in the request headers.

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

SOLUTION: I feel really stupid, turns out the particular jquery.js file I was pointing to was 1.4.4, so I upgraded and now it works! Thanks.

解决方案:我觉得非常愚蠢,结果我指向的特定jquery.js文件是1.4.4,所以我升级了,现在它可以工作了!谢谢。

1 个解决方案

#1


4  

It says:

它说:

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.

要与请求一起发送的其他标头键/值对的映射。在调用beforeSend函数之前设置此设置;因此,可以从beforeSend函数中覆盖标题设置中的任何值。

So all you have to do is to pass an object like:

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

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

and so on. Here is some code that adds Accept header in a post request:

等等。以下是一些在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"
});

#1


4  

It says:

它说:

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.

要与请求一起发送的其他标头键/值对的映射。在调用beforeSend函数之前设置此设置;因此,可以从beforeSend函数中覆盖标题设置中的任何值。

So all you have to do is to pass an object like:

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

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

and so on. Here is some code that adds Accept header in a post request:

等等。以下是一些在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"
});