David Jungman
David Jungman

Reputation: 13

Ajax with 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

Upvotes: 1

Views: 2439

Answers (2)

tlarson
tlarson

Reputation: 473

Actually, this worked for me:

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

Upvotes: 0

Deniz Aktürk
Deniz Aktürk

Reputation: 362

like this javascript code in html:

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

and use variable name in js file

Upvotes: 1

Related Questions