In my rails app I send request in html, ajax, xml and json. For xml and json I do it by writing :format => 'json/xml', for ajax we write :remote => true.
But how server know that it has to respond with ajax, xml or json? One thing I noticed when I send json or xml request it show in the url like below
http://localhost:3000/en/line_items.json
Though Im not sure, this is how server know if he has to give json response. But for html and ajax something like this is don't add in the url. So how server know whether it is ajax or html request?
Request headers are governed by mime-types , which basically determine which type of content / request is being sent
Rails has an in-built handler to manage different responses. As per @hawk's URL on ActionDispatch::Request , you can see this middleware essentially catches the type of request sent from the browser
Respond_To
The real answer to your question is about the respond_to
block:
#apps/controller/your_controller.rb
def new
respond_to do |format|
format.html { # do stuff here }
format.js { # do stuff here }
format.json { # do stuff here }
end
end
This will allow you to send various responses depending on the mime-type sent in the 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.