Skip to main content
 首页 » 编程设计

jquery之将 Accept header 添加到 JQuery AJAX GET(通过 JSONP)请求

2026年07月28日8zhoujg

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

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 上的文档网站,但我找不到任何好的例子。

这是我得到的请求 header 。我需要接受 header 为“application/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 

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

请您参考如下方法:

不幸的是,无法在 JSONP 请求上设置 header 。 JSONP 请求是通过添加 <script> 来完成的。标记到网站,然后浏览器像任何其他脚本一样加载该网站。 See this explanation of JSONP.