1

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 Answers 2

1

like this javascript code in html:

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

and use variable name in js file

Sign up to request clarification or add additional context in comments.

Comments

0

Actually, this worked for me:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.