I need to check if the request is ajax. $request->isXmlHttpRequest()
works fine, however if there is a redirect somewhere during the execution, this method will return false. How else can I check if request is ajax in this case?
p.s. ajax is initiated by jQuery
3 Answers
If you arent redirecting to a different application in your project or another external uri just use forward instead if isXmlHttpRequest is true on the first request.
Well that method checks against the value of the X-Requested-with
header and in some browser implementations that header (or all original headers) are dropped/overwritten from the redirected request (FF to name one).
As a workaround you could use a variable in the request itself. You might be able to use the existing sf_format
infrastructure, but im not sure if that would work because im not familiar with how it works internally.

- 34,448
- 50
- 182
- 322

- 60,050
- 10
- 100
- 114
-
Exactly, Firefox is to blame. Tested the same in Opera, works fine. – Dziamid Jan 11 '11 at 15:42
-
@Dziamid: Yeah, there is a bug filed but its missed the FF4 release, so its going to be at least 4.1 before its fixed. Additionally, there may be other browsers where this happens as well though im not aware of any others off hand. So i would play it conservative with your workaround. Actually if possibly i might remove redirect from the equation all together if possible. – prodigitalson Jan 11 '11 at 16:13
-
I'm banning my head against it, but to no avail. Obviously, setting a custom request parameter to a request object doesn't help - they are clear over a redirect. Using sf_format is a complicated way (because symfony will then look for different templates, like indexSuccess.sf_format.php).. – Dziamid Jan 11 '11 at 17:18
-
1Well you would need to encode that param in the url you pass to redirect. Are you redirecting to a different application in your project? Otherwise why not jsut use `forward` instead if `isXmlHttpRequest` is true on the first request? – prodigitalson Jan 11 '11 at 17:25
-
You can override that in the action(s) though by returning `sfView::NONE` and using `setLayout(false)` if `sf_format` is whatever youre using for ajax. But i mean using sf_Format isnt really all that special its doing the same thing as manually adding some arbitrary param youre going to use to detect ajax - its just already there. – prodigitalson Jan 11 '11 at 17:34
-
Exactly! I've just found it here: http://www.matthewbull.net/2008/05/11/symfony-redirect-vs-forward/. Using forward is the simplest solution. Thank you for you help. – Dziamid Jan 11 '11 at 17:36
-
@prodigitalson, please, put your comment about forward to you answer, so I can accept it. – Dziamid Jan 11 '11 at 17:38
If you need an easy workaround, I would suggest
Use a URL like http://mywebsite.com/loadsomething.php?ajax=true and
$isAjax = $request->getParameter('ajax');
Or, if you are POSTing you can create a hidden field named "ajax" . Ofcourse these won't solve your problem forever but will work as a quick fix if you need this as soon as possible.
If you want to support only one redirect, you can create a flash variable as well.
Otherwise you can look at the source code of symfony:
http://trac.symfony-project.org/browser/branches/1.4/lib/request/sfWebRequest.class.php
On line 518 you can see the request object identifies the request to be ajax from the HTTP headers. So you have to find out that after the redirect why the same HTTP headers are not set properly.

- 3,654
- 1
- 21
- 18
-
because they are cleared by sertain browsers, as @prodigitalson (see another answer). The problem has nothing to do with Symfony. – Dziamid Jan 11 '11 at 16:45
-
No, I didn't say it has anything to do with Symfony. I only said where would I start chasing the problem. – Aston Jan 11 '11 at 18:48