duanfang5849 2017-08-23 08:40
浏览 55
已采纳

如何将变量从javascript传递给symfony2控制器

I have a modal with some datas in a table and a radio button beside each row. Everytime I click that radio button, the corresponding datas will be stored in an array after clicking a button. Now, I have a problem in passing a variable from the javascript to my controller which is the 6th index of my array.

Based on what I have researched, most of the solutions were using ajax. I have already tried all the possible codes that I can in my controller but seems like it's not receiving anything. The data I sent in my ajax call did not reach the controller. Here is my twig file:

<button type="button" id="selectrow" class="btn btn-primary pull-right btn-spacing">Select</button>
$(document).ready(function () {
        $('#selectrow').click(function () {
          var data = selected[6];

                jQuery.ajax({
                    url: "project/edit/",
                    type: "GET",
                    data: { "data": data },
                    success: function (data) {
                        alert("OK");
                    },
                    error: function (data) {
                        alert("fail");
                    }
        });
});

The selected variable is where I stored the selected data somewhere in the file respectively. And below is my controller, I tried to display the following but the outputs are all null.

public function editAction(Request $request) {
       if ($request->isXmlHttpRequest()) {
          dump($request->getContent());
       }    

       dump($request->request->get('data'));
       dump($request->query->get('data'));
}

I am just really confused because after clicking the button it alerts 'OK' however, the data is not being sent to the controller. Can someone help me with this? It will really be appreciated, thank you.

This is the output that I get:

Output

  • 写回答

1条回答 默认 最新

  • doulong1987 2017-08-23 12:09
    关注

    The problem here is that you refresh the page which means that you call the controller without any data at all. What happens before that is that jQuery calls your editAction method and waits for a response that never appears because you return nothing in the Controller which is peculiar because you should get an Error saying that a Controller should return a Response.

    So what you have to ways to make sure the Controller receives the data from jQuery:

    1. dump and die:

      public function editAction(Request $request) {
          if ($request->isXmlHttpRequest()) {
              dump($request->getContent());
          }    
          dump($request->request->get('data'));
          dump($request->query->get('data'));
          die(); // terminate the current script
      }
      
    2. Return the data in a response and handle it in jQuerys success:

      In the Controller:

      return new JsonResponse($request->request->get('data'));
      

      In jQuerys success:

      $(document).ready(function () {
              $('#selectrow').click(function () {
              var dataSend = selected[6];
      
                      jQuery.ajax({
                          url: "project/edit/",
                          type: "GET",
                          data: { "data": dataSend },
                          success: function (data) {
                              console.log(data...);
                              if(data == dataSend) console.log("equivalent");
                          },
                          error: function (data) {
                              alert("fail");
                          }
              });
      });
      

      I don´t know what type the data variable. In case it is just a String or a Number you can just console.log(data) which should output the value in the browser console.

    Please leave a comment if that works...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?