weixin_33719619 2018-06-02 01:17 采纳率: 0%
浏览 49

Ajax与Symfony 4

I'm trying to connect Ajax with Symfony 4. I need to send some data to my Controller.

JavaScript:

$(function() {
    $('#sortable').sortable({
        axis: 'y',
        opacity: 0.7,
        update: function(event, ui) {
            var list_sortable = $(this).sortable('toArray').toString();

            $.ajax({
                url: 'path(app_bundle_route)',
                type: 'POST',
                dataType: 'json',
                data: {list_order:list_sortable},
                success: function(data) {
                    console.log("xyxy");
                }
            });
        }
    });
});

Controller:

/**
  * @Route("/admin/pages/reorder", name="admin_pages_reorder")
  * @return \Symfony\Component\HttpFoundation\Response
 */
 public function reorder(Request $request):Response
 {
   var_dump($request->getContent());
   die;
 }

I'm getting only 404 on this:

http://localhost:8000/admin/path(admin_pages_reorder).

Not even adding braces {{ path(...) }} helped.

I've solved that right after placing this question.

You have to place {{ path(route name) }} right into .twig, not external .js file

  • 写回答

2条回答 默认 最新

  • weixin_33713707 2018-06-03 20:22
    关注

    like this javascript code in html:

    var requestPath = '{{path(app_bundle_route)}}';
    

    and use variable name in js file

    评论

报告相同问题?