【问题标题】:Pass accepts header parameter to jQuery AJAX将接受标头参数传递给 jQuery AJAX
【发布时间】:2012-09-10 07:40:36
【问题描述】:

当我在 Chrome 控制台中检查以下代码时,它会显示一个请求标头 Accept:undefined

jQuery.ajax({
        url: _this.attr('href'),
        accepts: "application/json; charset=utf-8",
        
    });
});

如何将接受类型设置为 JSON。我不想设置自定义标题或使用beforeSend

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    试试这个,

    $.ajax({     
      headers: {          
        Accept: "text/plain; charset=utf-8",         
        "Content-Type": "text/plain; charset=utf-8"   
      }     
      data: "data",    
      success : function(response) {  
        // ...
      }
    });
    

    请参阅此帖子以供参考:

    Cannot properly set the Accept HTTP header with jQuery

    【讨论】:

    【解决方案2】:

    有两种设置accept header的方法,如下:

    1) setRequestHeader('Accept','application/json; charset=utf-8');
    
    2) $.ajax({
        dataType: ($.browser.msie) ? "text" : "json",
        accepts: {
            text: "application/json"
        }
    });
    

    【讨论】:

    • IE 的 'text' 值让我很生气。
    【解决方案3】:

    在最近的 jQuery 版本中,将“dataType”设置为适当的值也会设置接受头。例如,dataType: "json" 将接受标头设置为 Accept: application/json, text/javascript, */*; q=0.01

    【讨论】:

      【解决方案4】:

      其他答案没有回答实际问题,而是提供了解决方法,这很可惜,因为实际上需要 10 秒才能找出 accepts 参数的正确语法。

      accepts 参数接受一个将dataType 映射到Accept 标头的对象。在您的情况下,您甚至不需要传递 accepts 对象,因为将数据类型设置为 json 就足够了。但是,如果您确实想配置自定义 Accept 标头,您可以这样做:

      accepts: {"*": "my custom mime type" },

      我怎么知道?打开jquery的源代码并搜索“accepts”。第一个发现告诉你所有你需要知道的:

          accepts: {
              "*": allTypes,
              text: "text/plain",
              html: "text/html",
              xml: "application/xml, text/xml",
              json: "application/json, text/javascript"
          },
      

      如您所见,默认映射到 texthtmlxmljson 数据类型。

      【讨论】:

        【解决方案5】:

        试试这个:

        $.ajax({
                beforeSend: function (xhr){ 
                xhr.setRequestHeader("Content-Type","application/json");
                xhr.setRequestHeader("Accept","text/json");
            }, 
            type: "POST",
            //........
        });
        

        【讨论】:

        • 这是我唯一可以使用的解决方案,我使用 jQuery 2.0+
        【解决方案6】:

        虽然其中一些是正确的,但我发现之前的回答相当混乱。同时,OP 要求解决方案而不设置自定义标头或使用beforeSend,因此我一直在寻找更清晰的解释。我希望我的结论能给其他人一些启发。

        代码

        jQuery.ajax({
            .... 
            accepts: "application/json; charset=utf-8",
            ....
        });
        

        不起作用,因为根据 jQuery 文档 (https://api.jquery.com/jquery.ajax/),accepts 必须是 PlainObject(不是 String)。具体来说,jQuery 期望零个或多个键值对将每个 dataType 与它们接受的 MIME 类型相关联。所以我最终使用的是:

        jQuery.ajax({
            ....
            dataType: 'json',
            accepts: {
                json: 'application/json'
            },
            ....
        });
        

        【讨论】:

        • 这对我不起作用(jQuery 1.9.1)。 Accept 标头设置为 *.*(至少在 Chrome 和 Firefox 中)。使用 karthick 的“标题”选项确实有效。
        • 是的,没错,它是PlainObject,但没有解释键应该是什么......
        【解决方案7】:

        您已经将accepts参数确定为您想要的参数,并且keyur正确地向您展示了设置它的正确方法,但是如果您将DataType设置为“json”,那么它会自动将accepts的默认值设置为根据the jQuery reference,您想要的值。所以你只需要:

        jQuery.ajax({
            url: _this.attr('href'),
            dataType: "json"
        });
        

        【讨论】:

          【解决方案8】:

          我使用jQuery.getJSON( url [, data ] [, success( data, textStatus, jqXHR ) ] ) 例如:

          var url="my.php";
          $.getJSON( url, myObj )
          .done(function( json ) { ... }) /* got JSON from server */
          .fail(function( jqxhr, textStatus, error ) {
              var err = textStatus + ", " + error;
              console.log( "Failed to obtain JSON data from server: " + err );
            }); /* failed to get JSON */
          

          getJSON 是以下的简写:

          $.ajax({
            dataType: "json",
            url: url,
            data: data,
            success: success
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-18
            • 1970-01-01
            • 1970-01-01
            • 2012-11-18
            • 2010-12-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多