Forum: Ruby on Rails How do I recognize AJAX requests

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dc218395c242f9feb76c62aea1183874?d=identicon&s=25 helzer (Guest)
on 2007-05-18 19:08
(Received via mailing list)
Is there any way I can recognize that a request is an AJAX
(something_remote_xxx) request?

My application saves the pages history and needs to filter out the
AJAX requests. What I do is keep a list of AJAX actions and filter
them from my history list, but I always forget to update this list.

So, is there any way I can detect that a request is an AJAX one from
the request parameters?

Thanks,
Amir
52cb4115a870ff1942e7e1ae96f4306f?d=identicon&s=25 Tyler MacDonald (Guest)
on 2007-05-18 19:12
(Received via mailing list)
helzer <amir.helzer@gmail.com> wrote:
> So, is there any way I can detect that a request is an AJAX one from
> the request parameters?

Yup,

  if request.xhr?
    # this is an AJAX request
  else
    # this isn't an AJAX request
  end

  Cheers,
    Tyler
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-05-18 19:26
(Received via mailing list)
That's the old way. You're now supposed to do:

respond_to do |wants|
  wants.html { .. } # normal request
  wants.js { ... } # Ajax request
  ...
end

http://api.rubyonrails.org/classes/ActionControlle...

Jason
41c16e2bd2dd7efcbb7981e433471fda?d=identicon&s=25 Herryanto Siatono (jugend)
on 2007-05-18 19:30
(Received via mailing list)
Amir,

Try:

if request.xhr?
  ...
end

Herry
Dc218395c242f9feb76c62aea1183874?d=identicon&s=25 helzer (Guest)
on 2007-05-23 16:21
(Received via mailing list)
Thanks,

Works great.

Amir
Dc218395c242f9feb76c62aea1183874?d=identicon&s=25 helzer (Guest)
on 2007-05-23 16:21
(Received via mailing list)
Thanks,

Works great.

Amir
This topic is locked and can not be replied to.