I also had trouble with this, not just in IE but also in Chrome and Safari using jQuery 1.6.2. This solution appears to work as intended in all browsers I've tried (Chrome, Safari, IE, Firefox).
$.ajax({
headers: {
Accept : "text/plain; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
},
data: "data",
success : function(response) {
...
}
})
Try that if this is still giving you trouble.
Using jQuery 1.5+ you can set the accepts headers per dataType
so you can do something like this:
$.ajax({
dataType: ($.browser.msie) ? "text" : "xml",
accepts: {
xml: "text/xml",
text: "text/xml"
}
});
Your problem seems to be the one described here: http://www.grauw.nl/blog/entry/470. The issue is that the XMLHttpRequest specification currently states that user agents should not set any Accept headers by default for the request, so that req.setRequestHeader() can just append new Accepts. Unfortunately browsers don't yet adhere to this. The problem writeup lets you test your browser to see if it works properly, and unfortunately IE7, Chrome, Safari, Firefox, and Opera all fail.
Laurens Grauw also talks about the effects of first trying to null out the Accept header with
setRequestHeader('Accept', '')
or
setRequestHeader('Accept', null)
These might help here.
Ugly server-side hacks: If you have control over your server-side app you can hardwire it to always return XML, add support for a custom media type like "application/i-really-want-xml", or add support for a custom HTTP header like "X-Accept".
I think the original poster might have been referring to this link: http://blogs.msdn.com/ieinternals/archive/2009/07/01/IE-and-the-Accept-Header.aspx however, this doesn't explain the behavior you see.
IE does not, by itself, have the behavior you describe, and setting the Accept header via XMLHTTPRequest should work properly. I've tested in IE8 to confirm.
It's possible there's an issue in your version of jQuery, or perhaps you have some plugin mangling your traffic?
Although this isnt how the documentation states it needs to be done, it is what worked for me.
jQuery.ajax({
type: "POST",
url: "...",
data: ...,
contentType: "text/xml",
beforeSend: function(req) {
req.setRequestHeader("Accept", "text/xml");
}, ...});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With