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

前端 未结 6 616
旧时难觅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

    If you need to check if it was an Ajax request you can check for a header[X-Requested-With] using request.xhr?

    Example code snippet

    if request.xhr?
      # Do the ajax stuff
    else
      # Do normal stuff
    end
    

提交回复
热议问题