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

前端 未结 6 556
旧时难觅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条回答
  •  野的像风
    2021-02-03 23:59

    A list of ways to check for json

    request.format == 'json'
    request.format == :json
    request.format.json?
    request.path.match('json')
    request.url.match('json')
    respond_to do |format|
      format.json { render json: [] }
    end
    

    You can also check if request was an ajax request

    request.xhr?
    

提交回复
热议问题