Ruby on Rails:如何检测是否通过 JSON 访问? [英] Ruby on Rails: How do detect if access by JSON?

查看:52
本文介绍了Ruby on Rails:如何检测是否通过 JSON 访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个调用控制器方法 getInfo 的 GET URL.可以通过 mydomain.com/getInfo.json?params=BLAHBLAH 或 mydomain.com/getInfo?params=BLAHBLAH

I got a GET URL that calls a controller method getInfo. It can be called by mydomain.com/getInfo.json?params=BLAHBLAH or mydomain.com/getInfo?params=BLAHBLAH

在控制器中,有没有办法检测用户如何调用它?

In the controller, is there a way to detect how the user called it?

推荐答案

在你的控制器的特定方法中,你可以响应不同的格式——例如:

Within your controller's specific method, you can respond to different formats - for example:

respond_to do |format|
  format.html
  format.json { render :json => @user }
end

您可以响应各种格式(xml、js、json 等) - 您可以在此处获取有关 response_to 以及如何在控制器中使用它的更多信息:

You can respond to various formats (xml, js, json, etc) - you can get more information about respond_to and how to use it in your controller here:

http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

这篇关于Ruby on Rails:如何检测是否通过 JSON 访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆