Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a browser execute AJAX response in rails?

When we:

  1. Create a button_to with remote: true
  2. Put javascript code in a .js.erb file corresponding to the request

Then the code in the js.erb file gets executed on the response. I was curious as to how that code gets executed in the browser.

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

like image 674
Yeonho Avatar asked Jul 20 '15 13:07

Yeonho


People also ask

How rails send data using AJAX?

Basic way to perform AJAX operation Note 1 : Be sure with the dataType used in the code, it could be of type ' json ' or ' text ' depending on the data that needs to be sent or operation to perform in the controller end. return render json: { random_param_name: “Random event created successfully!”} And that's it.

How does AJAX return an API call?

ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.

What happens when JavaScript makes an AJAX request in a browser?

When you make an AJAX request, your browser sends an HTTP request to a given address. The server on the other end of the request responds, and returns the data to your browser. This is the same thing that happens when you navigate to a new web page.


1 Answers

It is executed, because the dataType of the ajax request is set to script.

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

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

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

like image 69
Yury Lebedev Avatar answered Sep 27 '22 15:09

Yury Lebedev