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

Viewed 19171

Let's consider a scenario like below:

  1. User selects filter button which create a AJAX call to a symfony2 controller and return the result in JSON format.
  2. User select some other links and system will redirect him to the page
  3. User select browser Back button.
  4. User will see JSON response, but he should see the original page.

My controller look like below :

/**
 * 
 *
 * @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 user press back button the if($request->isXmlHttpRequest()) returns true which is not the result I am looking for. Is it a normal behavior or what ?

3 Answers
Related