Symfony $request->isXmlHttpRequest() issue

后端 未结 3 979
情深已故
情深已故 2021-02-20 02:15

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

相关标签:
3条回答
  • 2021-02-20 03:03

    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.

    0 讨论(0)
  • 2021-02-20 03:07

    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.

    0 讨论(0)
  • 2021-02-20 03:14

    Some js libraries doesn't send the XmlHttpRequest value in the headers, juste add this in your script:

    xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
    
    0 讨论(0)
提交回复
热议问题