Set AJAX header header in request from IE

Is it possible to set the request header of the http content type for 'application / json' when sending the jquery ajax http request to Internet Explorer?

We are trying to use the WCF REST service, which interprets the content type from the request header when formatting the response. Right now, no matter what we put in the request header, it always returns data in XML format.

We tried using the jquery.iecors.js plugin, which extends the jQuery ajax call to use the XDomainRequest object, but it still ignores the content type that is set in our ajax jQuery 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 }); } 
+4
source share
2 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) }); 
+8
source

Yes, you can use the contentType parameter:

 $.ajax({ url: '/someurl', type: 'POST', contentType: 'application/json', data: JSON.stringify({ foo: 'bar' }), success: function(result) { } }); 

Request has been sent:

 POST /someurl HTTP/1.1 Host: example.com Content-Length: 13 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11 Content-Type: application/json Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 {"foo":"bar"} 
+2
source

Source: https://habr.com/ru/post/1403665/


All Articles