jQuery, jRails and the Accept header

Posted by Moser on 18 Jun 2009

I included jQuery by installing the jRails plugin, which seems nice because you can continue using rails’ ajax helpers. But when I tried to implement a more special functionality by using the $.ajax function it proved impossible to set the Accept header for my request. Neither using the dataType option nor by setting it directly in a beforeSend function. The Accept header always read:

text/javascript, text/html, application/xml, text/xml, */*

I was about to uninstall and hate jQuery for the rest of my life. By coincidence I took a look at jrails.js which is a part of the jRails plugin:

          xhr.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*")

WTF? Is that documented anywhere? This hard coded shit stuff breaks rails’ respond_to functionality, doesn’t it?t To be fair, it works if you use extensions to determine what datatype you expect. (Like /things/1.js) But I don’t do that when I build a custom ajax request where I can set the Accept header directly.

Update: Some research on the topic “accept header vs. extension” showed that in terms of cross browser compatibility I should favor the extension approach. And I’m not surprised it’s Microsoft’s fault :-)

blog comments powered by Disqus