jQuery, jRails and the Accept header

Posted by on June 18, 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:

(function($)
{
  $().ajaxSend(
    function(a,xhr,s){
      xhr.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*")
    }
  )
}
)(jQuery); 
[...]

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 :-)

Comments

Respond | Trackback

Comments

Comments: