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

图片转代码服务由CSDN问答提供 功能建议

我有一个模式,表格中有一些数据,每行旁边有一个单选按钮。 每次单击该单选按钮,相应的数据将在单击按钮后存储在数组中。 现在,我将一个变量从javascript传递给我的控制器,这是我的数组的第6个索引。

根据我研究的内容,大多数解决方案都使用ajax 。 我已经在我的控制器中尝试了所有可能的代码,但似乎没有收到任何东西。 我在ajax调用中发送的数据没有到达控制器。 这是我的twig文件:

 &lt; button type =“button”id =“selectrow”class =  “btn btn-primary pull-right btn-spacing”&gt;选择&lt; / button&gt; 
   
 
 
  $  (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“); 
},
 错误:函数(数据){
 alert(“fail”); 
} 
}); 
}); 
   
 
 

所选变量是 我将所选数据分别存储在文件中的某个位置。以下是我的控制器,我试图显示以下内容,但输出都是空的。

public function editAction(Request $ request){ if($ request-&gt; isXmlHttpRequest()){ dump($ request-&gt; getContent()); } 转储 ($ request-&gt; request-&gt; get('data')); dump($ request-&gt; query-&gt; get('data')); }

我真的很困惑,因为点击按钮后会发出“OK”警告,但数据没有发送到控制器。 有人可以帮我弄这个吗? 非常感谢,谢谢。

这是我得到的输出:

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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...

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题