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.
helzer (Guest)
on 2007-05-18 21: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
Tyler MacDonald (Guest)
on 2007-05-18 21:12
(Received via mailing list)
helzer <removed_email_address@domain.invalid> 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
Jason R. (Guest)
on 2007-05-18 21: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
Herryanto S. (Guest)
on 2007-05-18 21:30
(Received via mailing list)
Amir,

Try:

if request.xhr?
  ...
end

Herry
helzer (Guest)
on 2007-05-23 18:21
(Received via mailing list)
Thanks,

Works great.

Amir
helzer (Guest)
on 2007-05-23 18:21
(Received via mailing list)
Thanks,

Works great.

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