简体   繁体   中英

Ruby on Rails, json vs js ajax response

我很好奇什么是响应ajax请求的最佳方式,是发送json,然后在客户端解析它(例如使用pure )或者我应该在服务器端渲染javascript并返回结果(使用一个js.erb模板)?

Using js.erb templates tends to be simpler and encourages you to keep your application logic in your Rails controllers. For traditional applications, where little to no application logic is handled in javascript, this can be a good thing.

On the other hand, using JSON encourages you to develop your Rails app more as an API server for a javascript-heavy client application. If you're looking to do your core CRUD actions over ajax and rarely reload the page, this is probably fits better with your application's style.

Which is best in a particular situation depends on both the type of application you're looking to build and the attributes of the particular request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM