# How to check whether AJAX request or not

**URL:** <https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776>\
**Category:** Rails\
**Created:** [September 19, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776 "2009-09-19T13:39:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 19, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776/1 "2009-09-19T13:39:07Z")

</div>

Hello,

I don’t really know how to solve following problem: When I access  
…/users/1 I get the website and the user is shown properly, all good.  
I have some link\_to\_remote actions on my website and when they call the  
/users/1 action I - of course - get the whole page rendered despite I  
just wanted to update a div with only name being displayed. My question  
now is how it’s possible to determine within the action if it’s an HTTP  
or AJAX request and render accordingly (full layout for HTTP and only  
one DIV box for the AJAX request).

How would you solve that kind of problem?

Thank you,  
Heinz

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 19, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776/2 "2009-09-19T14:29:58Z")

</div>

On Sep 19, 12:39 pm, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

I’m sure you know this but AJAX requests are of course http  
requests :-).

There’s the xml\_http\_request? method (also available as xhr? ) and of  
course respond\_to is designed for this sort of stuff.

Fred

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 22, 2009, 3:54am UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776/3 "2009-09-22T03:54:07Z")

</div>

Hi Heinz,

On Sat, 2009-09-19 at 13:39 +0200, Heinz S. wrote:

> How would you solve that kind of problem?

I may be misunderstanding your question but I think you just need to  
wrap the rendering, and potentially some of the actual logic, in a  
respond\_to block.

respond\_to do |format| {  
format.html { … }  
format.js { …}  
end

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 19, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776/4 "2009-09-19T14:34:04Z")

</div>

Frederick C. wrote:

> On Sep 19, 12:39ï¿½pm, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 
> 
> I’m sure you know this but AJAX requests are of course http  
> requests :-).
> 
> There’s the xml\_http\_request? method (also available as xhr? ) and of  
> course respond\_to is designed for this sort of stuff.
> 
> Fred

Yeah I know I just didn’t no how to call these two requests 🙂 I’ll have  
a look at that xhr? thing. Thanks Frederick!

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [September 23, 2009, 5:03am UTC](https://www.ruby-forum.com/t/how-to-check-whether-ajax-request-or-not/175776/5 "2009-09-23T05:03:27Z")

</div>

On Mon, Sep 21, 2009 at 9:53 PM, bill walton [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > just wanted to update a div with only name being displayed. My question  
> > respond\_to do |format| {  
> > format.html { … }  
> > format.js { …}  
> > end

respond\_to selects what’s to be rendered not on whether it’s an ajax  
request or not, but by the acceptable mime types in the request. An  
ajax request might be requesting html.

request.xhr? is the proper way to detect an ajax request.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
