How do I recognize AJAX requests


#1

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


#2

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


#3

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/ActionController/MimeResponds/InstanceMethods.html#M000080

Jason


#4

Thanks,

Works great.

Amir


#5

Amir,

Try:

if request.xhr?

end

Herry