【问题标题】:Add Accept header to JQuery AJAX GET (via JSONP) request将 Accept 标头添加到 JQuery AJAX GET(通过 JSONP)请求
【发布时间】:2014-03-24 19:43:38
【问题描述】:

我正在尝试向使用“jsonp”dataType 的 jQuery AJAX GET 请求添加接受标头,但由于某种原因它无法正常工作。到目前为止,这是我的代码。

var e4json =  JSON.stringify( { 
                  "thomas_smith106"           : "Daniel244", 
                  "transaction_type"          : "34",
                  "transaction_tag"           : "902006933",
                  "authorization_num"         : "ET4653",
                  "amount"                    : "15.75"
              } ); 

$.ajax ({
    url: "https://api.demo.globalgatewaye4.firstdata.com",
    type: "GET",
    headers : {
        'accepts' : 'application/json'
    },
    data: e4json,
    dataType: "jsonp",
    success: function  (response) {
        alert('Successfuly called the e4 gateway api');
    }
});

我尝试了多种方法,但似乎没有任何效果。我查看了jQuery 网站上的文档,但找不到任何好的示例。

这是我的请求标头得到的。我需要接受标头为“应用程序/json”。

Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_fd_session=d69310c5cd4a02a4700b5ba63f0d0c9b
Host:api.demo.globalgatewaye4.firstdata.com
Referer:http://localhost:8080/smart-two-site/customerinfo.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36

任何帮助都会很棒。谢谢!

【问题讨论】:

    标签: jquery ajax get jsonp http-accept-header


    【解决方案1】:

    很遗憾,无法在 JSONP 请求中设置标头。 JSONP 请求是通过向网站添加<script> 标记来完成的,然后浏览器会像任何其他脚本一样加载该标记。 See this explanation of JSONP.

    【讨论】:

      【解决方案2】:

      使用这个;

      headers: {          
           Accept : "application/json; charset=utf-8",         
          "Content-Type": "application/json; charset=utf-8"   
      } 
      

      【讨论】:

        【解决方案3】:

        我想你想这样设置:

              headers: {          
                     Accept : "application/json; charset=utf-8",         
                    "Content-Type": "application/json; charset=utf-8"   
              }   
        

        【讨论】:

        • 当你说你需要设置接受头?当然,服务器是通过您的请求接受内容的,因此您需要设置内容类型。您想总体实现什么目标?
        • @Jordan 'Content-Type' 标头指示请求的类型(因此不用于 GET)。服务器使用它来将请求正文解码为有用的内容,或者在无法处理时拒绝请求。 “Accept”标头是响应中接受的类型列表,可能按偏好加权。服务器使用此列表来选择要发送的响应类型或在无法生成预期响应类型之一时拒绝请求。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-07
        • 2018-11-13
        • 2023-03-12
        相关资源
        最近更新 更多