简体   繁体   中英

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

render json: results 

How do I return both JSON data and HTML data to be processed in JQuery? and Where do I store this view file?

You can use respond_to in the controller. 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).

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

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