将Accept标头添加到JQuery AJAX GET(通过JSONP)请求

时间:2022-08-22 11:07:25

I'm trying to add an accept header to a jQuery AJAX GET request which uses the "jsonp" dataType, but for some reason it's not working. Here's my code thus far.

我正在尝试将一个接受标头添加到使用“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');
    }
});

I've tried multiple things but nothing seems to be working. I looked at the the documentation on the jQuery site, but I'm not able to find any good examples.

我尝试了很多东西,但似乎没有任何工作。我查看了jQuery网站上的文档,但我找不到任何好的例子。

This is what I get for my request headers. I need the accept header to be 'application/json'.

这是我的请求标题。我需要接受标头为'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

Any help would be awesome. Thanks!

任何帮助都是极好的。谢谢!

3 个解决方案

#1


3  

Unfortunately, it is not possible to set headers on a JSONP request. A JSONP request is done by adding a <script> tag to the website, which the browser then loads like any other script. See this explanation of JSONP.

不幸的是,无法在JSONP请求上设置标头。通过向网站添加

#2


-1  

Use this;

用这个;

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

#3


-2  

I think you want to be setting it like so:

我想你想这样设置它:

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

#1


3  

Unfortunately, it is not possible to set headers on a JSONP request. A JSONP request is done by adding a <script> tag to the website, which the browser then loads like any other script. See this explanation of JSONP.

不幸的是,无法在JSONP请求上设置标头。通过向网站添加

#2


-1  

Use this;

用这个;

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

#3


-2  

I think you want to be setting it like so:

我想你想这样设置它:

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