weixin_33697898 2019-07-28 20:48 采纳率: 0%
浏览 48

Symfony 3.4中的Ajax请求

i am using symfony 3.4 , i want to execute a controller action on a button click in my twig template , that's why i used ajax for that : first i added the ajax route :

ajax_route:
    path:     /ajax_request
    defaults: { _controller: offerBundle:Default:ajax }
    methods: [post]

then the controller action :

    public function ajaxAction(Request $request)
    {
        $personnage = $request->request->get('personnage');
        dump($request->request);
        $wishlist = new wishlist();
        $wishlist->setUserid($personnage);
        $wishlist->setOfferid(1);
        $em=$this->getDoctrine()->getManager();
        $em->persist($wishlist);
        $em->flush();
    }

the ajax part :

    <script>
        $('.btn').click( function(){
            var personnage = 3;
            $.ajax({
         url: "{{ path('ajax_route') }}",
         type: "POST",
                dataType: "json",
                data: {
                    "personnage": personnage
                },
                async: true,
                success: function (data)
                {
                    console.log(data)
                }
            })
        });

    </script>

this is my html button :

<button id="wishlist" class="btn">add to wishlist</button>

what happens when i click on the button is i do get this error log :

1 AJAX request Method :POST Type:xhr Status:500 URL:/ajax_request

and the 'var_dump' in the controller dosen't display anything

  • 写回答

1条回答 默认 最新

  • weixin_33691700 2019-07-29 05:09
    关注

    Your mistake is on the route.

    path(): Generates a relative URL path given a route name and parameters.

    url() : Generates an absolute URL given a route name and parameters.

    in this line use

    url: "{{ url('ajax_route') }}",
    

    Instead

    url: "{{ path('ajax_route') }}",
    

    and your controller

    use Symfony\Component\HttpFoundation\JsonResponse;
    *
    *
    
          public function ajaxAction(Request $request)
             $arrayAjax = array("position" => "fasle");
            {
          if (($request->getMethod() == Request::METHOD_POST) && ($request->isXmlHttpRequest())) {
    
                $personnage = $request->request->get('personnage');
                dump($request->request);
                $wishlist = new wishlist();
                $wishlist->setUserid($personnage);
                $wishlist->setOfferid(1);
                $em=$this->getDoctrine()->getManager();
                $em->persist($wishlist);
                $em->flush();
                $arrayAjax = array("position" => "true");
             }
        return new JsonResponse($arrayAjax2);
           }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100