Symfony2 AJAX Login

前端 未结 7 2750
谎友^
谎友^ 2020-11-28 01:42

I have an example where I am trying to create an AJAX login using Symfony2 and FOSUserBundle. I am setting my own success_handler and failure_handler

7条回答
  •  隐瞒了意图╮
    2020-11-28 02:43

    This may not be what the OP asked, but I came across this question, and thought others might have the same problem that I did.

    For those who are implementing an AJAX login using the method that is described in the accepted answer and who are ALSO using AngularJS to perform the AJAX request, this won't work by default. Angular's $http does not set the headers that Symfony is using when calling the $request->isXmlHttpRequest() method. In order to use this method, you need to set the appropriate header in the Angular request. This is what I did to get around the problem:

    $http({
        method  : 'POST',
        url     : {{ path('login_check') }},
        data    : data,
        headers: {'X-Requested-With': 'XMLHttpRequest'}
    })
    

    Before you use this method, be aware that this header does not work well with CORS. See this question

提交回复
热议问题