简体   繁体   English

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

[英]How to pass a variable from javascript to symfony2 controller

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. 现在,我在将变量从javascript传递到控制器(这是数组的第6个索引)时遇到问题。

Based on what I have researched, most of the solutions were using ajax. 根据我的研究,大多数解决方案都使用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. 我在ajax调用中发送的数据未到达控制器。 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: 这是我得到的输出:

输出量

The problem here is that you refresh the page which means that you call the controller without any data at all. 这里的问题是您刷新了页面,这意味着您根本没有任何data调用了控制器。 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 . 在此之前发生的事情是jQuery调用了您的editAction方法并等待一个永远不会出现的响应,因为您在Controller中什么也没有返回,这是奇怪的,因为您会得到一条错误消息,说明Controller should return a Response

So what you have to ways to make sure the Controller receives the data from jQuery : 因此,您必须采取什么方法来确保Controller从jQuery接收data

  1. dump and die : dumpdie

     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 jQuery s success : 返回响应中的data ,并在jQuery success

    In the Controller: 在控制器中:

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

    In jQuery s success: jQuery的成功中:

     $(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. 我不知道data变量的类型。 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. 如果它只是一个字符串或数字,则可以只提供console.log(data) ,它应该在浏览器控制台中输出该值。

Please leave a comment if that works... 如果可行,请发表评论...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM