Is $ request-> isXmlHttpRequest () reliable in symfony2?

Consider the scenario described below:

  • The user selects a filter button that creates an AJAX call to the symfony2 controller and returns the result in JSON format.
  • The user selects some other links, and the system redirects him to the page
  • Button to select a browser for the user.
  • The user will see the JSON response, but he should see the source page.

My controller is as follows:

/** * * * @Route("/ajax", name="ajax_route" , options={"expose"=true}) * @Template() */ public function someAction() { $request = $this->getRequest(); $json = array( ); if($request->isXmlHttpRequest()) { $res = json_encode($json); return new Response($res , 200 , array( 'Content-Type' => 'application/json' )); } return array( ); } 

In other words, if the user presses the return button, if($request->isXmlHttpRequest()) returns true, which is not the result I'm looking for. Is this normal behavior or what?

+7
source share
3 answers

Symfony\Component\HttpFoundation\Request::isXmlHttpRequest() is a simple utility method that checks if an HTTP request has appeared with an X-Requested-With header with an XMLHttpRequest value. So it is reliable as the X-Requested-With header.

However, this is not very important. It is important to note that when the user clicks the "Back" button, the browser does not send a new HTTP request to the server. It just restores the page from its internal memory / cache.

+6
source

I understand that this is an old question, but the same problem just caught me, so I decided that I would write the answer anyway.

In most scenarios, you can invalidate the feedback button cache by connecting the onUnload handler to the window, for example:

 window.addEventListener('unload',function(){}); 

or, if you prefer jQuery:

 $(window).unload(function(){}); 

but since your AJAX response is in JSON, this is obviously not possible, since you cannot include script fragments. In this case, I believe that it is best to set the cache-control: no-store header so that the browser does not try to cache the result.

You can do this in the case of OP with Symfony2 using:

 return new Response($res , 200 , array( 'Content-Type' => 'application/json', 'Cache-Control' => 'no-store', )); 

or for more general PHP:

 header('Cache-Control: no-store'); 

There is a caveat here that can greatly degrade your performance, depending on the structure of your application, in which case the best option would probably be to just use a different url for your AJAX call. Sucks, I know.

Here you can find bfcache documentation, which may be more useful in different cases.

+4
source

The browser caches the response using only the URL and the request method (GET, POST, etc.) as the key.

If you want the browser to recognize additional options, you can say that this is necessary by setting the Vary header in the response. Therefore, in your case, you want to tell the browser that the response from the server will depend on whether the "X-Requested-With" header was set in the request.

Here's how to do it:

 $response = new Response(); $response->setVary("X-Requested-With"); // <=========== Set the Vary header if($request->isXmlHttpRequest()) { //... } return $response; 

Note: you want to set the Vary header in both versions of the answer (so I installed it outside the if statement).

0
source

All Articles