How does a browser execute an AJAX response in rails?

When we:

  • Create button_to with remote: true
  • Put the javascript code in the .js.erb file matching the request

Then the code in the js.erb file js.erb launched in response. I was curious how this code runs in a browser.

Is this some kind of eval call in the rails library, or is it related to the Content-Type header set in text/javascript in the response?

+6
source share
2 answers

Executes because the dataType for the ajax request has a script value.

 $.ajax({ url: url, dataType: "script", success: success }); 

http://api.jquery.com/jquery.ajax/

http://api.jquery.com/jquery.getscript/

+1
source

Rails generates javascript code for sending the call, and the jquery_ujs.js file is added to your layout by default. Then it simply converts all calls with the data-remote=true attribute - it prevents the form from submitting by default and makes an ajax call to the specified href .

0
source

All Articles