我正在尝试使用Jquery将两个javascript数组发布到我的控制器,但它不想访问我的数组。
$('#reservez2').on("click",function(){
$.ajax({
method:"POST",
url:connexion,
data:{
'depart' : coordoneesDepart,
'arrive' : coordoneesArrive,
},
success: function(data)
{
alert("succes !");
}
})
});
那是我的javascript函数
/**
* @Route("/connexion", name="connexion")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
if($request->isXmlHttpRequest()){
$depart=$request->request->get('depart');
$arrivee=$request->request->get('arrivee');
}
dump($depart);
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/connexion.html.twig',[
'error'=>$error,
'last_username'=>$lastUsername,
]);
}
这就是我的控制器
我的网址来自那是
var connexion = $('.routeConnexion').data('routeController');
在我看来,这是该div的链接
<div class="routeConnexion" data-route-controller="{{path('connexion')}}"></div>
据我所知,您的处理程序无法识别您传递给它的数据(两个数组)。我怀疑以JSON格式发送数据可能是前端最简单的方法,前提是您的Web框架支持它(必须这样做,对吧?)
$.ajax({
method:"POST",
url:connexion,
contentType: "application/json",
data: JSON.stringify({
depart: coordoneesDepart,
arrive: coordoneesArrive,
}),
success: function(data) {
alert("Success!");
}
})
注意,我添加了contentType
,并且正在使用JSON.stringify
将您的数据转换为JSON格式的字符串。前者将告诉服务器有效负载中的数据类型,后者将确保您发送的数据是有效的JSON。
要指出的另一件事是您的处理程序中的这一行:
$arrivee=$request->request->get('arrivee');
get
调用将不起作用,因为您拼错了arrive
;我们不会向服务器发送名为arrivee
的变量。]>