1

I am trying to send crossdomain ajax GET request

$(document).ready(function(){
    $.ajax
      ({
        type: "GET",
        url: "url",
        cache: false,
        contentType: "application/x-www-form-urlencoded",
        dataType: "jsonp",
        beforeSend: function (xhr){ 
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
            xhr.setRequestHeader('Authorization', make_base_auth("login", "pwd")); 
        },
        success: function (data, success){
            console.log("success", arguments); 
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log("error", arguments);
        },
        complete: function(jqxhr, textStatus) {
          console.log("complete", arguments);
        }
    });
});

The server only accepts application/x-www-form-urlencoded data. After sending the request I receive response with 415 status (Unsupported media type) When I check headers in firebug, I don't see Content-Type header at all.

GET /url?callback=jsonp1374315852923&_=1374315854258 HTTP/1.1
Host: hostname
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://refferer
Cookie: JSESSIONID=1DCD5D9EB35FEF083C7EAF8F7EA25D54; ltime=1374315850; lid=30000379384174; SESS826f62d511330ecdaa9034d04e399c65=qj2jcempp4osi2hc00lq1jnkp1; AA_SESS=2c-30f9987cd946; AA_UID=0fe8965d05c37706a5bfbec88629ed68
Authorization: Basic YWthdml0YTpQNlBhdQ==
Connection: keep-alive

Would appreciate your comments very much!

  • 1
    jsonp isn't really ajax, it inserts a script tag into the DOM, that's all, so unless the service you are contacting supports this, it won't magically work by setting a header. – adeneo Jul 20 '13 at 10:47
  • "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation." [docu](http://api.jquery.com/jQuery.ajax/) – mkutyba Jul 20 '13 at 11:13

1 Answers1

0

You are using JSONP as your data type. JSONP causes jQuery to simply insert a script tag into your document, which will then get pulled in as if it was a normal browser request. As a result, you cannot set headers on it.

If you do not have a specific need to use JSONP (you are using the same domain), then use JSON instead. If you are making request across domains, look into using CORS (which may not work in older browsers).

Peter
  • 1,674
  • 4
  • 27
  • 44
  • You may also want to refer to http://stackoverflow.com/questions/3350778/modify-http-headers-for-a-jsonp-request – Peter Jul 20 '13 at 19:47