Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set AJAX content type header in request from IE

Is it possible to set the http content-type request header to 'application/json' when sending a cross domain jquery ajax http request from Internet Explorer?

We're trying to hit a REST WCF service that interprets the content type from the request header when formatting the response. Right now, no matter what we put in the request header it is always returning the data in XML format.

We've tried using the jquery.iecors.js plugin which extends the jquery ajax call to use the XDomainRequest object but that is still ignoring the content-type that is set in our jquery ajax call.

Here's what our ajax call looks like:

makeGETRequest: function (requestUrl) {
    return $.ajax({
        type: "GET",
        url: requestUrl,
        contentType: 'application/json',
        dataType:'json',
        cache: false
    });
}
like image 413
KodeKreachor Avatar asked Mar 27 '12 13:03

KodeKreachor


1 Answers

Just pass the content-type as one of your parameters to the .ajax method:

var retval = jQuery.ajax({
    type:'post',
    url: url,
    contentType: 'application/json',
    data: JSON.stringify(data)
});
like image 150
slashingweapon Avatar answered Sep 21 '22 17:09

slashingweapon