[英]Cannot access javascript variable inside symfony controller
On my symfony (v3.4) project, I need to pass some javascript variables from my view to my controller containing a form : I used Jquery and Ajax to send my variable to the right route but for some reason I cannot access it from my controller. 在我的symfony(v3.4)项目中,我需要将一些javascript变量从视图传递到包含表单的控制器:我使用Jquery和Ajax将变量发送到正确的路径,但是由于某些原因,我无法从我的变量访问它控制器。
Here is the script part in my twig view: 这是我的树枝视图中的脚本部分:
$(".month").click(function() {
var click = $(this);
var month = click.val();
var year = $("#years").val();
var url = "{{ path('av_platform_saisie') }}";
$.ajax(url, {
type: 'POST',
data: {
'month': month,
'year': year
},
success: function(data) {
alert('OK');
},
error: function(jqXHR, textStatus, errorThrown) {}
});
});
And my controller: 而我的控制器:
public function saisieAction(Request $request)
{
$user = $this->getUser();
$thisyear = date("Y");
$em = $this->getDoctrine()->getManager();
// Create the form
$form = $this->get('form.factory')->createBuilder(FormType::class)
->add('ndf', CollectionType::class, array(
'entry_type' => NoteDeFraisType::class,
'label' => false,
'allow_add' => true,
'allow_delete' => true,
))
->getForm();
// if the form has been submited
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
if($request->isXMLHttpRequest()){
$month = $request->get('month');
$year = $request->get('year');
$sub_date = $month .'/' .$year;
}
$notesDeFrais = $form['ndf']->getData();
foreach ($notesDeFrais as $ndf) {
$ndf->setUser($user);
$ndf->setMonth($sub_date);
$em->persist($ndf);
}
$em->flush();
}
return $this->render('AvPlatformBundle:Platform:saisie.html.twig',
array(
'year' => $thisyear, 'form' => $form->createView()
));
}
The weird thing is: I tried to send my js variables from a new Twig view that is not rendered by my controller and it's working just fine with exactly the same code so maybe it is a normal behavior ? 奇怪的是:我试图从一个新的Twig视图发送我的js变量,该视图不是由我的控制器渲染的,并且在完全相同的代码下也能正常工作,所以也许这是正常现象吗?
EDIT: I did some debugging and actually and the code inside my if($request->isXMLHttpRequest())
is not executed 编辑:我做了一些调试,实际上和我
if($request->isXMLHttpRequest())
中的代码未执行
You're not using the right syntax to get your parameters, this is how it should be : 您没有使用正确的语法来获取参数,这应该是这样的:
if($request->isXMLHttpRequest()) {
$month=$request->request->get('month');
$year=$request->request->get('year');
$sub_date=$month .'/' .$year;
}
And in case you want to get GET
parametrers, it's this : 如果您想获取
GET
参数,就是这样:
$request->query->get('get_param');
You have to get the content of the Request correctly. 您必须正确获取请求的内容。
Depending on your version of Symfony, other answers might not work and you might have to use: 根据您的Symfony版本,其他答案可能不起作用,您可能必须使用:
$request->getContent(); $ request-> getContent();
To have access to the associative array containing your data. 有权访问包含您的数据的关联数组。
(Edit to add my comment): If it does not work, the only other thing on top of my head would be that your IDE autoimport would have imported the wrong "Request" class in your controller ? (编辑以添加我的评论):如果不起作用,那么,我脑中唯一的另一件事就是您的IDE自动导入会在控制器中导入错误的“ Request”类? Make sure you use --> use Symfony\\Component\\HttpFoundation\\Request;
确保使用->使用Symfony \\ Component \\ HttpFoundation \\ Request;
Put your url inside the array passed to ajax with, like so: 将您的网址放在传递给ajax的数组中,如下所示:
$(".month").click(function() {
var click = $(this);
var month = click.val();
var year = $("#years").val();
$.ajax({
url: "{{ path('av_platform_saisie') }}",
type: 'POST',
data: { 'month': month,
'year': year
},
success: function (data) {
alert('OK');
},
error : function(jqXHR, textStatus, errorThrown){}
});
});
Did you checked the source code of the form, what is the value of action
attribute? 您是否检查过表单的源代码,
action
属性的值是什么? I think you can't add the js url
for that AJAX request as you did. 我认为您不能像您那样为该AJAX请求添加js
url
。 Ideally, install FosJsRoutingBundle , and modify as: 理想情况下,安装FosJsRoutingBundle ,并修改为:
url: Routing.generate('av_platform_saisie')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.