Php AJAX登录无法映射用户名-始终为空
我正在尝试从普通登录表单迁移到AJAX。没有AJAX,一切都可以完美运行,但当我尝试实现它时,出现了一个错误-Php AJAX登录无法映射用户名-始终为空,php,symfony,Php,Symfony,我正在尝试从普通登录表单迁移到AJAX。没有AJAX,一切都可以完美运行,但当我尝试实现它时,出现了一个错误-键“\u username”必须是字符串,如果给定了“/code>”,则为“NULL”。为什么会发生这种情况 我相信用户名不能被映射,但为什么?AJAX请求设置正确,它发送_用户名和_密码。我试图将它们转换为字符串,但没有任何更改 登入表格: <form id="loginForm"> <div class="form-group"> &l
键“\u username”必须是字符串,如果给定了“/code>”,则为“NULL”。为什么会发生这种情况
我相信用户名不能被映射,但为什么?AJAX请求设置正确,它发送_用户名和_密码。我试图将它们转换为字符串,但没有任何更改
登入表格:
<form id="loginForm">
<div class="form-group">
<input type="text" class="form-control inner-blue-shadow" id="username" name="_username" placeholder="Username"/>
</div>
<div class="form-group">
<input type="password" class="form-control inner-blue-shadow" id="password" name="_password" placeholder="Password"/>
</div>
<div class="text-center"><button type="submit" id="loginButton">Log in</button></div>
</form>
安全.yaml
form_login:
login_path: login
check_path: login
success_handler: authentication_handler
failure_handler: authentication_handler
处理程序:
namespace AppBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return new Response("Some success response...");
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
var_dump($exception);
if ($exception instanceof DisabledException) {
return new JsonResponse([
'message' => 'The account is disabled.'
]);
} else {
return new JsonResponse([
'message' => 'Password or username is incorrect.'
]);
}
}
}
登录控制器:
/**
* @Route("/login", name="login")
* @return RedirectResponse|Response
*/
public function login()
{
$user = $this->getUser();
if($user) {
$userRoles = $user->getRoles();
if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
if(in_array(Role::ROLE_STUDENT, $userRoles)) {
return $this->redirectToRoute('dashboard_student_home');
}
}
}
return $this->render('security/login.html.twig');
}
问题的解决方案是,作为一个数据,我必须传递整个表单:
const form = document.getElementById("loginForm");
const data = new FormData(form);
axios.post('{{path('login')}}', data);
通过这种方式,所有内容都可以正确提交—用户名、密码,甚至CSRF。您可以在controller中共享登录操作吗?当然可以。我已经更新了帖子。您在哪里使用用户名?我希望它是登录操作中的一个参数。您确定页面上只有一个id=“username”
元素吗?ID必须是唯一的。如果有多个元素具有此id,那么jquery可能会从另一个元素而不是您想要的元素中提取空值。如果执行console.log(用户名、密码),您会得到什么代码>就在常量之后?
const form = document.getElementById("loginForm");
const data = new FormData(form);
axios.post('{{path('login')}}', data);