# Request.xhr? vs. respond\_to

**URL:** <https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325>\
**Category:** Rails\
**Created:** [June 24, 2006, 7:44am UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325 "2006-06-24T07:44:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 7:44am UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/1 "2006-06-24T07:44:46Z")

</div>

Searched around this forum, but didn’t find an answer for this question.

Can you help this newbie understand any overlap and/or difference  
between request.xhr? and respond\_to?

If request.xhr? is true, should I expect it wants.js below?  
respond\_to do |wants|  
wants.html { redirect\_to(person\_list\_url) }  
wants.js  
wants.xml { render :xml =\> @person.to\_xml(:include =\> @company) }  
end

Is it fair to say the functionality of respond\_to a super set of  
request.xhr?

TIA!

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 11:47am UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/2 "2006-06-24T11:47:48Z")

</div>

short answer: no, not equivalent

longer answer:

– request.xhr? –

check the following header in the request:

```
    'X-Requested-With' = 'XMLHttpRequest',

```

this is set automatically by the prototype library

see line 699 in version 1.5.0\_rc0

– response\_to –

check the ‘Accept’ header. this is set not only by prototype but also  
by browser, spiders, etc.

looks something like ‘Accept’, ‘text/html, application/xml,  
text/xml, _/_’]

check

Doug D. schrieb:

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/3 "2006-06-24T18:24:10Z")

</div>

Thank you, Pete, for the explanation of where they come from!

To implement graceful _RJS_ degradation, does it mean request.xhr? is  
sufficient? Or need both? Is it possible that a broswer doesn’t set the  
Accept header accordingly? See  
[http://www.ruby-forum.com/topic/65059#74833](http://www.ruby-forum.com/topic/65059#74833)

For _JS_ (in general) degradation, however, respond\_to is the way to go?

Doug (quite ignorant about Web UI)

Pete wrote:

> short answer: no, not equivalent
> 
> longer answer:
> 
> – request.xhr? –
> 
> check the following header in the request:
> 
> ```
> 'X-Requested-With' = 'XMLHttpRequest',
> 
> ```
> 
> this is set automatically by the prototype library
> 
> see line 699 in version 1.5.0\_rc0
> 
> – response\_to –
> 
> check the ‘Accept’ header. this is set not only by prototype but also  
> by browser, spiders, etc.
> 
> looks something like ‘Accept’, ‘text/html, application/xml,  
> text/xml, _/_’]
> 
> check
> 
> Doug D. schrieb:

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/4 "2006-06-24T18:29:25Z")

</div>

my understanding is that request.xhr? indicates the controller has  
received an ajax request, while respond\_to indicates that the browser  
can accept specific mime types.

non?

Jodi

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/5 "2006-06-24T19:16:00Z")

</div>

I’m not sure I can speak specifically to degradation…Alas I’m  
building for a defined Intranet userbase, so I have more  
predictability than the average cat.

I use request.xhr? to target particular responses to the browser - a  
partial for ajax, a partial for ‘non-ajax’.

…having said that it makes a certain amount of sense that accepts  
(respond\_to) would is the appropriate question you want answered :  
‘can the browser understand an ajax response’.

so yes, I agree - the latter seems to be the appropriate approach…

though others more experience than me may add to the argument.

good luck Adam.

Jodi

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [June 24, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/request-xhr-vs-respond-to/64325/6 "2006-06-24T18:56:35Z")

</div>

Yes. Thank you, Jodi!

Here is my confusion: Doesn’t this mean that if we get request.xhr?  
true, RJS will be stuffed back regardless whether the browser accepts  
JS? That’s because the request comes from the prototype library. But, I  
see JS degradation is implemented with respond\_to:  
[Graceful degrading AJAX show/hide - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/64357#72054). But again, another fellow  
had some trouble: [AJAX request not setting Accept header - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/65059#74833)

Which one or both should be used to determine what the browser is able  
to or want to handle? Perhaps, we should respect just the latter?

Jodi S. wrote:

> my understanding is that request.xhr? indicates the controller has  
> received an ajax request, while respond\_to indicates that the browser  
> can accept specific mime types.
> 
> non?
> 
> Jodi
