dongshu4755 2013-12-28 05:27
浏览 57
已采纳

Ajax调用Controller和XML响应Symfony2

I'm new to Symfony2. Now I want to implement my previous practice for ajax call using Symfony2.

I want to make an ajax call to a server side code and get XML response and extract it. Here is what I do.

var data={
            type:'1'                           

        };

        jQuery.ajax({
                  url: 'server_script/check.php',  //load data 
                  global: false,
                  type: "POST",
                  dataType: "xml",
                  data: data,
                  async: false, 
                  success: load_data2
        });

In flat php file I can catch that and using

$type=$_POST['type'];

In Symfony2 I'm making AJAX call by using

           jQuery.ajax({
                          url: '{{ path('create_label') }}',  //load data 
                          global: false,
                          type: "POST",
                          dataType: "xml",
                          data: data,
                          async: false, 
                          success: load_data2,
                          error: problem
                    });

And in Controller that comes from the create_label,I catch it using

$type->request->get('type');

I'm not sure yet though if it's really working. Got it from this link Link

Next in flat PHP I produce a XML response by

  header("Content-type: text/xml");
            echo "<?xml version=\"1.0\" ?>
";
            echo "<Loadinglist>
";
            echo "</Loadinglist>";

It gave me a response that I extract in function load_data2(xmlindata) method.

Now I'm not able to make any valid XML response in Symfony2. I tried few tricks but none of them are working. I want to send a XML response from Controller , catch it and extract in with the load_data2 method. If you have time please provide an wiki type answer that how the xml response work is done.

(N.B. In flat php I could check the XML response in console or firebug, will I be able to view the same in Symfony2)

  • 写回答

1条回答 默认 最新

  • dozpv84422 2013-12-28 09:26
    关注

    To get the POST parameter, it's better to be explicit and use $request->request->get('type'). This will only get the POST parameter 'type' and not the GET parameter 'type'.

    Next, a controller always returns a Response. This class can be seen as a PHP port of the real HTTP response. So you can tweak it to return XML.

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    class YourController // ...
    {
        function someAction(Request $request)
        {
            // ...
    
            $response = new Response(<<<EOX
    <?xml version="1.0" ?>
    <Loadinglist>
    </Loadinglist>
    EOX
            );
            $response->headers->set('Content-Type', 'text/xml');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配