如何将 Ajax 与 Symfony2 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13584591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:53:09  来源:igfitidea点击:

How to integrate Ajax with Symfony2

ajaxsymfony

提问by Thomas Shelby

I'm looking form simple tutorial/example about ajax in symfony2, for beginner?

我正在为初学者寻找关于 symfony2 中 ajax 的简单教程/示例?

I have these examples:

我有这些例子:

How can these be put into a Symfony2 app?

如何将这些放入 Symfony2 应用程序中?

回答by JeanValjean

It is easy. I will illustrate how to do an AJAX call in Symfony2 through 3 steps. For the following example, assume to use the jQuery library.

这很容易。我将通过 3 个步骤来说明如何在 Symfony2 中进行 AJAX 调用。对于以下示例,假设使用 jQuery 库。

  • Define the route for the action that has to handle your AJAX call. E.g.

    AcmeHomeBundle_ajax_update_mydata:
      pattern:  /update/data/from/ajax/call
      defaults: { _controller: AcmeHomeBundle:MyAjax:updateData }
    
  • Define the action in the MyAjaxcontroller from Homebundle. E.g.

    public function updateDataAction(){
      $request = $this->container->get('request');        
      $data1 = $request->query->get('data1');
      $data2 = $request->query->get('data2');
      ...
      //handle data
      ...
      //prepare the response, e.g.
      $response = array("code" => 100, "success" => true);
      //you can return result as JSON
      return new Response(json_encode($response)); 
    }      
    
  • Prepare your AJAXcall in your Twigtemplate, e.g.:

    function aButtonPressed(){
        $.post('{{path('AcmeHomeBundle_ajax_update_mydata')}}',               
                    {data1: 'mydata1', data2:'mydata2'}, 
                function(response){
                        if(response.code == 100 && response.success){//dummy check
                          //do something
                        }
    
        }, "json");    
    }
    
    $(document).ready(function() {     
      $('button').on('click', function(){aButtonPressed();});
    });
    

    You can change the example by using other AJAX calls.

  • 为必须处理 AJAX 调用的操作定义路由。例如

    AcmeHomeBundle_ajax_update_mydata:
      pattern:  /update/data/from/ajax/call
      defaults: { _controller: AcmeHomeBundle:MyAjax:updateData }
    
  • MyAjaxHomebundle 中定义控制器中的操作。例如

    public function updateDataAction(){
      $request = $this->container->get('request');        
      $data1 = $request->query->get('data1');
      $data2 = $request->query->get('data2');
      ...
      //handle data
      ...
      //prepare the response, e.g.
      $response = array("code" => 100, "success" => true);
      //you can return result as JSON
      return new Response(json_encode($response)); 
    }      
    
  • AJAX在您的Twig模板中准备您的电话,例如:

    function aButtonPressed(){
        $.post('{{path('AcmeHomeBundle_ajax_update_mydata')}}',               
                    {data1: 'mydata1', data2:'mydata2'}, 
                function(response){
                        if(response.code == 100 && response.success){//dummy check
                          //do something
                        }
    
        }, "json");    
    }
    
    $(document).ready(function() {     
      $('button').on('click', function(){aButtonPressed();});
    });
    

    您可以使用其他 AJAX 调用更改示例。