2017-01-24 36 views
1

我试图在单击按钮后使用AJAX运行我的控制器代码。从我收集的信息来看,这就是您应该使用AJAX的原因......它会从前端向后端发送响应,然后生成json响应。如何在Symfony2中正确使用AJAX

但我无法弄清楚如何正确地做到这一点。我不确定如何让AJAX成功运行控制器代码。

所有我已经能够做的就是展示在成功的表,但我不希望控制器代码运行按钮被点击,直到,因为这是AJAX的点。

的Symfony似乎并不对此有文档:http://symfony.com/legacy/doc/book/1_0/en/11-Ajax-Integration

而且这些堆栈溢出的问题/答案是太老了我的使用或不帮我:

How to integrate Ajax with Symfony2

How to make a POST Ajax request with Symfony and Jquery

嫩枝:

<button id="begin-check" type="button">Run Test</button> 

<table class="stab"> 
    <thead> 
    <tr> 
     <th style="text-align: center">Ang</th> 
     <th style="text-align: center">Lat</th> 
    </tr> 
    <tr> 
     <th>Name &amp; Age</th> 
     <th>Name &amp; Age</th> 
    </tr> 
    </thead> 
    <tbody> 
    {% for person in persons %} 
     <tr> 
      <td> 
       <a href="{{ path('users_edit', { 'id': person[0] }) }}"> 
        {{ person[1] }}, {{ person[2] }} 
       </a> 
      </td> 
      <td> 
      {% if person[7] == 'Moe' %} 
       <a href="{{ path('edit', { 'id': person[6] }) }}"> 
      {% else %} 
       <a href="{{ path('low_edit', { 'id': person[8] }) }}"> 
      {% endif %} 
        {{ person[4] }}, {{ person[5] }} 
       </a> 
      </td> 
     </tr> 
    {% endfor %} 
    </tbody> 
</table> 

AJAX:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $(document).on('click', "#begin-check", function(e){ 
      console.log('You have clicked'); 
      e.preventDefault(); 
      $.ajax({ 
       method: 'POST', 
       url: "{{ path('control_check') }}", 
       data: { //research what to put here 
       }, 
       success: function(responseData){ 
        //run the controller code 
        //Show HTML table 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        console.log(xhr.status); 
        console.log(xhr.responseText); 
        console.log(thrownError); 
       }, 
      }) 
     }); 
    }); 

</script> 

在控制器:

/** 
* 
* @Route("/control", name="control_check") 
* @Template() 
*/ 
public function controlCheckAction() 
{ 
    $request = $this->get('request'); 
    $em = $this->getDoctrine()->getManager(); 

    $persons = array(); 

    //more code 

    return new JsonResponse(array('persons' => $persons)); 

} 
+0

是你的javascript代码插入树枝模板吗? –

+0

它与上面的我的树枝在同一个文件中 – grgre

+0

您不能在JavaScript成功函数中运行严重的侧控制器代码。当调用成功时,responseData将拥有controlCheckAction生成的任何json。然后您需要javascript来渲染表格。看看你是否能找到一个好的介绍Ajax教程,而没有Symfony的复杂性。你需要首先理解基础知识。 – Cerad

回答

0

我想你需要添加到你已经拥有的是isXmlHttpRequest()方法。 Here的官方文档。

所以:

/** 
* 
* @Route("/control", name="control_check") 
* @Template() 
*/ 
public function controlCheckAction(Request $request) 
{ 
    if ($request->isXmlHttpRequest()) { 
     //$request = $this->get('request'); 
     //replace the up line code with the folloging, which gets a particular form field 
     $name = $request->request->get('name'); // <=> $name = $_POST['name'] 
     $em = $this->getDoctrine()->getManager(); 

     $persons = array(); 

     //more code 

     return new JsonResponse(array('persons' => $persons)); 
    } 
} 
+0

我肯定忘了将request $ request参数添加到控制器函数中......所以非常感谢你,但是我对$ request-> request-> get('name')会产生困惑。 ;'对我来说。我没有一个表单字段在这里使用... – grgre

+0

然后只需在'controlCheckAction()'中输入'dump($ request)',在按下提交按钮后查看可用的东西。我真的不知道你打算做什么。通常在模板中有一个'

',它通过AJAX调用发送它的数据。 –

0

也许,我遇到同样的情况。

就我而言,我忘了添加“使用”声明。 难道你忘了添加这一行吗?

use Symfony\Component\HttpFoundation\JsonResponse; 

我附加了我的检查点。

【1】检查服务器侧应用程序日志

error_log("create json response OK" . "\n", 3, "/usr/local/var/log/php/error.log"); 
return new JsonResponse(array('persons' => $persons)); 

【2】检查控制台调试器

开放铬调试器(F12) - >网络标签 - > “/控制”(左导航)

因此,您可以确认“响应标题”。

[OK] Content-Type:application/json 
[NG] Content-Type:text/html 

如果找到“text/html”,我想服务器端的问题。

【3】检查Symfony2的调试

你可以找到Symfony2的调试器的URL在同一标签【2】

X-Debug-Token-Link:http://xxxxxxxxx/_profiler/xxxxx 

你应该打开这个链接探查。 如果此问题来自服务器端,则可以发现任何问题。