为什么控制器在通过AJAX发布表单数据时不重定向到指定的路由?
我正在使用Slim3框架、用于管理模板的细枝和雄辩的ORM。我的AuthController.php如下所示为什么控制器在通过AJAX发布表单数据时不重定向到指定的路由?,ajax,post,model-view-controller,slim,Ajax,Post,Model View Controller,Slim,我正在使用Slim3框架、用于管理模板的细枝和雄辩的ORM。我的AuthController.php如下所示 class AuthController extends Controller{ public function getSignUp($request, $response){ return $this->view->render($response, 'auth/signup.twig'); } public function p
class AuthController extends Controller{
public function getSignUp($request, $response){
return $this->view->render($response, 'auth/signup.twig');
}
public function postSignUp($request, $response){
$user = User::create([
'firstname' => $request->getParam('first-name'),
'lastname' => $request->getParam('last-name'),
'email' => $request->getParam('email'),
'zipcode' => $request->getParam('zipcode'),
'phonenumber' => $request->getParam('phone-number'),
'username' => $request->getParam('username'),
'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT)
]);
return $response->withRedirect($this->router->pathFor('home'));
}
}
编写的AJAX函数如下所示
$(document).ready(function(){
$('#signup').click(function(event){
event.preventDefault();
$.ajax({
type: "post",
url: "{{ path_for('auth.signup') }}",
data: $(this).serialize()
});
});
});
php有以下两条与注册过程有关的路径
$app->get('/auth/signup', 'AuthController:getSignUp')->setName('auth.signup');
$app->post('/auth/signup', 'AuthController:postSignUp');
现在一切正常。数据通过AJAX正确发布并插入到“users”表中。但是这个代码
return $response->withRedirect($this->router->pathFor('home'));
不起作用。即使在发布之后,也不会发生重定向。如果表正在更新,这意味着postSignUp()函数中的插入代码正在工作。那为什么return语句不起作用呢
我对Slim 3框架和MVC范例都是相当陌生的,所以我不确定我做错了什么 为什么要在事后重定向用户时使用AJAX请求?您不能使用“普通”表单请求吗?请确保$this->router已正确初始化。