jQuery on Rails

jQuery on Rails
By jchris in Coding 4 months ago.

Ok this may be old news for some of you, but yesterday I wanted to send Javascript back to the client as part of an Ajax response, with a failure HTTP header. Specifically I wanted to send a flash message back when the user entered a wrong password on login. The details shouldn’t matter for you – you might expect jQuery to eval Javascript sent with a 4xx header just like it does with a success header.

Use this snippet and you’ll get that expected behavior:

$(window).bind('ajaxError', function (e, xhr, s) {
  if (s.dataType == 'script' && xhr.responseText) {
    $.globalEval(xhr.responseText);        
  }
});

Be the first to comment on jQuery on Rails

Post a comment