简体   繁体   English

在Rails中将HTML和JSON返回到jquery调用

[英]Returning HTML and JSON to jquery post call in Rails

In my rails application I need to return a html view to Jquery post() call that will be made by java script As of now I was returning JSON only and it was done in the following way 在我的Rails应用程序中,我需要返回将由Java脚本进行的JQuery post()调用的html视图。到目前为止,我仅返回JSON,它是通过以下方式完成

render json: results 

How do I return both JSON data and HTML data to be processed in JQuery? 如何同时返回要在JQuery中处理的JSON数据和HTML数据? and Where do I store this view file? 以及该视图文件存储在哪里?

You can use respond_to in the controller. 您可以在控制器中使用response_to。 And then depending on the format sent by the jQuery request, either will be executed (foo.json or just foo, though you can force the content type in jQuery too so check this). 然后根据jQuery请求发送的格式,将执行其中一个(foo.json或foo,尽管您也可以在jQuery中强制内容类型,所以请选中此选项)。

respond_to do |which|
  which.html # Renders the view
  which.json { render json: results }
end

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

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