How do I recognize AJAX requests

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

helzer [email protected] 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

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

Thanks,

Works great.

Amir

Amir,

Try:

if request.xhr?

end

Herry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs