简体   繁体   English

Ruby on Rails,json vs js ajax response

[英]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. 使用js.erb模板往往更简单,并鼓励您将应用程序逻辑保留在Rails控制器中。 For traditional applications, where little to no application logic is handled in javascript, this can be a good thing. 对于传统应用程序,在javascript中几乎没有应用程序逻辑处理,这可能是一件好事。

On the other hand, using JSON encourages you to develop your Rails app more as an API server for a javascript-heavy client application. 另一方面,使用JSON鼓励您更多地将Rails应用程序开发为用于javascript密集型客户端应用程序的API服务器。 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. 如果您希望通过ajax执行核心CRUD操作并且很少重新加载页面,这可能更适合您的应用程序的样式。

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. 在特定情况下哪种情况最好取决于您要构建的应用程序类型以及特定请求的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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