简体   繁体   English

如何在ajax调用中将数组传递给控制器​​并使用Symfony3返回

[英]Howto pass array in ajax call to controller and return it with Symfony3

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. 您好,感谢您的帮助,我进行了一个ajax调用,并希望将其发送到Controller,以便以后可以删除指定的数据。 For the moment I would like to show the given javascript from the controller. 目前,我想从控制器中显示给定的javascript。 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? 它当前正在返回JsonResponse以测试是否调用了该方法,但是我想返回给定的javascript数组(播放日志),我该怎么做?

Javascript: 使用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: PlayLogController中的deleteBulkAction:

/**
 * @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);
}

Use 'request' ParameterBag to access POST, PUT, PATCH, and DELETE data 使用'request'ParameterBag访问POST,PUT,PATCH和DELETE数据

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

and 'query' - for GET data 和“查询”-用于GET数据

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

You can send playlogs as json (or any other format, depends on data). 您可以将播放日志作为json发送(或任何其他格式,取决于数据)。 Read request body by $request->getContent() and decode it in php array. 通过$request->getContent()读取请求正文,并将其解码为php数组。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数组作为参数从JavaScript中的ajax调用传递给控制器 - Pass an array as parameter to a controller from an ajax call in JavaScript 如何使用Symfony3和Ajax将tinyMCE textarea编辑器内容发送到内部控制器 - How can I send tinyMCE textarea editor content to inside controller using Symfony3 and Ajax 如何使用ajax调用获取数组列表数据(将数组列表数据控制器传递给ajax方法) - How to get array list data using ajax call (pass array list data controller to ajax method) 克鲁德ajax jQuery symfony3将无法正常工作 - crud ajax jquery symfony3 wont work 为什么ajax返回代码500 [symfony3] - Why ajax returns code 500 [ symfony3 ] Symfony3发送AJAX POST请求 - Symfony3 send AJAX POST request AJAX 无法将数组传递给 controller - AJAX is failing to pass array to controller 通过 Ajax 调用将数组发送到 controller,然后 controller 在 Laravel 中返回另一个包含该数组的视图 - Send array to controller by Ajax call, then controller return another View with that array in Laravel 将复选框数组传递到AJAX调用 - Pass Checkbox Array into AJAX call 我正在尝试使用 ajax 将数组从 controller 传递到 javascript 但它不会以数组形式返回,而是以字符串形式返回 - I'm trying to pass an array from controller to javascript by using ajax but it doesnt return as array it returns as a String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM