5

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.

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.

This is what I get for my request headers. I need the accept header to be '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!

Perseids
  • 12,584
  • 5
  • 40
  • 64
adeiji
  • 550
  • 1
  • 3
  • 13

3 Answers3

4

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.

Perseids
  • 12,584
  • 5
  • 40
  • 64
-1

Use this;

headers: {          
     Accept : "application/json; charset=utf-8",         
    "Content-Type": "application/json; charset=utf-8"   
} 
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
-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"   
      }   
Jordan
  • 359
  • 3
  • 12
  • When you say you need to set the accepts header? Surely the server is what is accepting the content through your request and therefore you need to set the content type. What are you trying to achieve overall? – Jordan Mar 25 '14 at 14:59
  • 2
    @Jordan The 'Content-Type' header indicates the type of the request (so not used for a GET). This is used by the server to decode the request body into something useful or reject the request if it cannot be handled. The 'Accept' header is a list of types that are accepted in the response with possible weighting by preference. The server uses this list to choose a response type to be sent or to reject the request if it cannot generate one of the expected response type. – Lee Saferite Jul 14 '15 at 12:38