development full of
merriment and sense

One Way I Got JQuery To Play Nicely With Rails

Geoffrey on September 13, 2007 at 10:51 am

Bridge
photo by dave_mcmt

I love JQuery! I did a short 10-15 minute presentation at the last Dallas.rb to let others in on the joys of using JQuery.

But one thing that doesn’t work right when using JQuery with Rails applications is the JQuery AJAX features and Rails respond_to. It turns out the Rails it looking for a specific request header, but JQuery sends something different one.

It is easily solved with this at the top of your application.js file:

$.ajaxSetup({
  beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
});

Filed under: Rails, JavaScript, JQuery, AJAX

Powered by WordPress