我有一个似乎不起作用的Ajax登录表单。
这是我试过的。
base.html.twig
<section>
<input type="text" id="_username" name="_username" placeholder="Email Address" />
<input type="password" id="_password" name="_password" placeholder="Password" />
<footer>
<button data-iziModal-close>Cancel</button>
<button id="form_login" class="submit">Log in</button>
</footer>
</section>
Javascript Ajax Call
$('#form_login').on('click', function(event) {
event.preventDefault();
$.ajax({
type : 'post',
url: '/login',
data: '_username=abc&_password=temp123',
success : function(response) {
console.log(response);
}
});
});
services.yml
services:
acme.security.authentication_handler:
class: UserBundle\Handler\AuthenticationHandler
public: false
arguments: ["@router", '@session']
security.yml
security:
encoders:
UserBundle\Entity\User: bcrypt
providers:
in_memory:
memory: ~
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
form_login:
check_path: login
success_handler: acme.security.authentication_handler
failure_handler: acme.security.authentication_handler
access_control:
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
UserBundle \处理程序\ AuthenticationHandler.php
<?php
namespace UserBundle\Handler;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $router;
private $session;
public function __construct(RouterInterface $router, Session $session)
{
$this->router = $router;
$this->session = $session;
}
public function onAuthenticationSuccess( Request $request, TokenInterface $token )
{
if ($request->isXmlHttpRequest()) {
$array = array('success' => true);
$response = new Response(json_encode($array));
$response->headers->set('Content-Type', 'application/json');
return $response;
} else {
if ( $this->session->get('_security.main.target_path')) {
$url = $this->session->get('_security.main.target_path');
} else {
$url = $this->router->generate('home');
}
return new RedirectResponse($url);
}
}
public function onAuthenticationFailure( Request $request, AuthenticationException $exception )
{
// if AJAX login
if ($request->isXmlHttpRequest()) {
$array = array('success' => false, 'message' => $exception->getMessage()); // data to return via JSON
$response = new Response(json_encode( $array ));
$response->headers->set('Content-Type', 'application/json');
return $response;
// if form login
} else {
// set authentication exception to session
$request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
return new RedirectResponse($this->router->generate('login'));
}
}
}
的appbundle \控制器\ DefaultController.php
/**
* @Route("/login", name="login")
* @Template()
*/
public function loginAction()
{
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
}
我从Ajax响应中得到以下错误
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'The controller for URI "/login" is not callable.
使用Ajax处理身份验证的正确方法是什么?我在哪里定义业务逻辑以返回错误或成功消息?
对冗长的问题表示感谢和道歉。