Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: format.js or format.json, or both?

Might be obvious, but still I'm lacking here of the basic knowledge.

So inside controllers, can both be used, or is it always Javascript, so both are the same?

like image 839
Ben Avatar asked Feb 25 '10 17:02

Ben


1 Answers

json and js are two different types of response and they are defined as different MIME types in Rails

Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript ) Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest ) 

Even if Json can be considered a subset of JavaScript, not all JavaScript responses are actually Json responses.

You might want to respond with Json and Js in the same action.

For instance, you might have an action that responds with Json to an API call and with JavaScript (perhaps using RJS) to an internal Ajax call.

like image 72
Simone Carletti Avatar answered Sep 25 '22 16:09

Simone Carletti