简体   繁体   English

Ajax调用Controller和XML响应Symfony2

[英]Ajax call to Controller and XML response Symfony2

I'm new to Symfony2. 我是Symfony2的新手。 Now I want to implement my previous practice for ajax call using Symfony2. 现在,我想使用Symfony2实现我以前的ajax调用实践。

I want to make an ajax call to a server side code and get XML response and extract it. 我想对服务器端代码进行ajax调用,并获取XML响应并将其提取。 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 平面php文件中,我可以捕获并使用

$type=$_POST['type'];

In Symfony2 I'm making AJAX call by using Symfony2中,我通过使用AJAX进行呼叫

           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 在来自create_label的Controller中,我使用

$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 接下来在平面PHP中,我通过生成XML响应

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

It gave me a response that I extract in function load_data2(xmlindata) method. 它给了我在function load_data2(xmlindata)方法中提取的响应。

Now I'm not able to make any valid XML response in Symfony2. 现在,我无法在Symfony2中进行任何有效的XML响应。 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. 我想从Controller发送一个XML响应,捕获它并使用load_data2方法提取。 If you have time please provide an wiki type answer that how the xml response work is done. 如果您有时间,请提供一个Wiki类型的答案,说明xml响应工作是如何完成的。

(NB In flat php I could check the XML response in console or firebug, will I be able to view the same in Symfony2 ) (注意,在平面php中,我可以在控制台或firebug中检查XML响应,我可以在Symfony2中查看该响应)

To get the POST parameter, it's better to be explicit and use $request->request->get('type') . 要获取POST参数,最好是显式的并使用$request->request->get('type') This will only get the POST parameter 'type' and not the GET parameter 'type'. 这只会获取POST参数“ type”,而不会获取GET参数“ type”。

Next, a controller always returns a Response . 接下来,控制器始终返回Response This class can be seen as a PHP port of the real HTTP response. 此类可以视为实际HTTP响应的PHP端口。 So you can tweak it to return XML. 因此,您可以对其进行调整以返回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');
    }
}

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

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