When we:
button_to
with remote: true
.js.erb
file corresponding to the requestThen 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?
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.
ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With