我的控制器中的 JS 变量问题

在我的 symfony (v3.4) 项目中,我需要将一些 javascript 变量从我的视图传递给我的控制器:我使用 Jquery 和 Ajax 将我的变量发送到控制器,但我无法访问我的变量。我的 Ajax 请求没有问题,我通过 Symfony 分析器进行了检查,请求已正确发送,但由于某种原因,控制器甚至无法检测到 Ajax 请求。


这是我的控制器:


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()){

                //After some code debuging, this is never 

                //executed

                $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()

            ));

    }

我的 saisie.html.twig 视图中的脚本:


$(".month").click(function() {



     var click = $(this);


     var month = click.val();

     var year = $("#years").val();


      $.post("{{ path('avaliance_platform_saisie') }}",

            { 'month' : month,

              'year' : year

            },


            function (data,status) {

                alert('Data sent');


            });

            


        });


UYOU
浏览 150回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript