# Serverside AjaxRequest or AjaxUpdater detections

**URL:** <https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457>\
**Category:** Rails\
**Created:** [June 17, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457 "2009-06-17T19:41:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [June 17, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457/1 "2009-06-17T19:41:16Z")

</div>

Hi all,

This question might be a long shot, but I’m going to ask it anyway. In  
my application, I’ve created a before\_filter that does some magic. It  
looks like this:

if request.xhr? then  
render :update do |page|  
page.redirect\_to url\_for\_thingy  
end  
else  
redirect\_to redirect\_to url\_for\_thingy  
end

This works perfectly, however, I have one annoyance. On the server side,  
I don’t know if the xhr request coming in is from an AjaxUpdater or  
AjaxRequest (using prototype here). It’s possible that the browser will  
update a DOM element or that it just expects some javascript code. When  
an AjaxUpdater is used, the element is updated and the javascript is  
executed (so the redirect is done), but it just doesn’t look so nice.

Anyone got a solution?

Greetings,

Wouter

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [June 17, 2009, 7:48pm UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457/2 "2009-06-17T19:48:43Z")

</div>

On Jun 17, 6:41 pm, Wouter de Bie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> This works perfectly, however, I have one annoyance. On the server side,  
> I don’t know if the xhr request coming in is from an AjaxUpdater or  
> AjaxRequest (using prototype here). It’s possible that the browser will  
> update a DOM element or that it just expects some javascript code. When  
> an AjaxUpdater is used, the element is updated and the javascript is  
> executed (so the redirect is done), but it just doesn’t look so nice.

You could look at the Accept header sent by the browser - in the  
Ajax.Request case it should be something like application/javascript

Fred

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [June 19, 2009, 10:55am UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457/3 "2009-06-19T10:55:40Z")

</div>

Frederick C. wrote:

> You could look at the Accept header sent by the browser - in the  
> Ajax.Request case it should be something like application/javascript

request.env[‘HTTP\_ACCEPT’] has the same value for a normal GET,  
AjaxUpdater or AjaxRequest…

Any other suggestions?

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [June 19, 2009, 11:04am UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457/4 "2009-06-19T11:04:24Z")

</div>

On Jun 19, 9:55 am, Wouter de Bie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> > You could look at the Accept header sent by the browser - in the  
> > Ajax.Request case it should be something like application/javascript
> 
> request.env[‘HTTP\_ACCEPT’] has the same value for a normal GET,  
> AjaxUpdater or AjaxRequest…
> 
> Any other suggestions?

Hack prototype to so that one of the two sets an http header of your  
choice ?

Fred

---

<div class="post-metadata">

**Author:** ![xinit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/9f8e36/32.png) [@xinit](https://www.ruby-forum.com/u/xinit)\
**Post date:** [June 19, 2009, 11:06am UTC](https://www.ruby-forum.com/t/serverside-ajaxrequest-or-ajaxupdater-detections/170457/5 "2009-06-19T11:06:34Z")

</div>

Frederick C. wrote:

> Hack prototype to so that one of the two sets an http header of your  
> choice ?
> 
> Fred

Yeah, I was thinking the same thing. Maybe adding an X\_HTTP\_FOO header  
is an nice option… Let’s see if I can do it so that it doesn’t break  
when upgrading Rails to a future release.
