jQuery ajax request not triggering JS response from rails controller? jQuery ajax request not triggering JS response from rails controller? jquery jquery

jQuery ajax request not triggering JS response from rails controller?


This solution didn't work for me (rails 3.1 + coffeescript). After searching quite a lot, I found the good way to do it and I wanted to share:

Just add ".js" to the end of the url. So simple... ;-)


Just add dataType: 'script'

$.ajax({  url: $("form#new_picture").attr("action"),  type: "POST",  data: formdata,  processData: false,  contentType: false,  dataType: 'script'});    


You have to set the 'accept' header before sending the ajax request so that Rails knows how to respond.

$.ajax({  url: $("form#new_picture").attr("action"),  type: "POST",  data: formdata,  processData: false,  contentType: false,  beforeSend: function(xhr, settings) {    xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);  }});