0

Hi and thanks for helping me, I have a ajax call with data I would like to send to the Controller so I can delete the specified data at a later time. For the moment I would like to show the given javascript from the controller. Sorry in advance for my English, it's not my first language.

It's currently returning a JsonResponse to test if the method is called but I want to return the given javascript array (playlogs) instead, how can I do that?

Javascript:

<script type="text/javascript">
       $(document).ready(function () {
           $('#deleteBtn').click(function () {
               var playlogs = [];

               $.each($("input[name='playlog']:checked"), function () {
                   playlogs.push($(this).val());
               });
               var confirmText = "Are you sure you want to delete this?";
               if (confirm(confirmText)) {
                   $.ajax({
                       type: "delete",
                       url: '{{ path('playlog_delete_bulk') }}',
                       data: playlogs,
                       success: function () {
                           alert("selected playlogs: " + playlogs.join(", "));
                       },
                   });
               }
               return false;
           });

       });
   </script>

deleteBulkAction in PlayLogController:

/**
 * @Route("/delete/bulk", name="playlog_delete_bulk")
 */
public function deleteBulkAction(Request $request)
{
    if ($request->isXMLHttpRequest()) {
        return new JsonResponse(array('data' => 'Successfully called the named route playlog_delete_bulk  '));
    }

    return new Response('This is not ajax!', 400);
}

2 Answers 2

1

You can send playlogs as json (or any other format, depends on data). Read request body by $request->getContent() and decode it in php array.

Ex.:

<script type="text/javascript">
   $(document).ready(function () {
       $('#deleteBtn').click(function () {
           var playlogs = [];
           $.each($("input[name='playlog']:checked"), function () {
               playlogs.push($(this).val());
           });

           var confirmText = "Are you sure you want to delete this?";

           if (confirm(confirmText)) {
               $.ajax({
                   type: "delete",
                   url: '{{ path('playlog_delete_bulk') }}',
                   data: JSON.stringify(playlogs),
                   success: function (data) {
                      alert("selected playlogs: " + data.join(", "));
                   }
               });
           }
           return false;
       });

   });

/**
 * @Route("/delete/bulk", name="playlog_delete_bulk")
 */
public function deleteBulkAction(Request $request)
{
    if ($request->isXMLHttpRequest()) {
        return new JsonResponse(json_decode($request->getContent()));
    }

    return new Response('This is not ajax!', 400);
}
Sign up to request clarification or add additional context in comments.

3 Comments

How do I decode it in a php array something like this: $array = JsonResponse(json_decode($request->getContent())); return $array;
I now get this error: "The controller must return a response (Array(0 => 25, 1 => 11, 2 => 24, 3 => 14, 4 => 17, 5 => 26) given, with: $array = json_decode($request->getContent()); var_dump($array); return $array;
controller should return Response. symfony.com/doc/current/controller.html
0

Use 'request' ParameterBag to access POST, PUT, PATCH, and DELETE data

  $password = $request->request->get('password');

and 'query' - for GET data

  $pageNumber = $request->query->get('page');

4 Comments

Ths is not working for me, I get a error 400. $array = $request->request->get('playlogs'); var_dump($array);
400 error has nothing to do with this. get() will either return parameter or NULL. What do you get with $request->getContent()?
I've set the type to POST and now it's working, but how do I use this "new response" in a php array? This throws me a bad request: $array = new Response($request->request->get('playlogs')); var_dump($array);
Oh my. Don't create new Response, use the one that Symfony sends into your action.

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.