Ruby on Rails: How do detect if access by JSON?

前端 未结 6 617
旧时难觅i
旧时难觅i 2021-02-03 23:43

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 cont

6条回答
  •  萌比男神i
    2021-02-03 23:52

    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
    

    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

提交回复
热议问题