Ruby on Rails, json vs js ajax響應

[英]Ruby on Rails, json vs js ajax response


I'm curious what is the best way to respond to an ajax request, is it sending json, and then parse it at the client side (for example with pure) or should I render javascript at the server side and return the result(with a js.erb template)?

我很好奇,響應ajax請求的最佳方式是發送json,然后在客戶端解析(例如使用pure),還是在服務器端呈現javascript並返回結果(使用js)。erb模板)?

1 个解决方案

#1


7  

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.

使用js。erb模板更簡單,鼓勵您將應用程序邏輯保存在Rails控制器中。對於傳統的應用程序來說,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. 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.

另一方面,使用JSON鼓勵您將Rails應用程序更多地開發為javascript密集型客戶端應用程序的API服務器。如果您希望在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.

在特定的情況下,最好的情況取決於您要構建的應用程序的類型和特定請求的屬性。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/12/09/21e2fcc539cfe139b57f7bde8ff76cd9.html



 
  © 2014-2022 ITdaan.com 联系我们: