如何在Symfony2中使用jquery/Ajax从PHP文件中获取数组字段值?

Nad*_*ram 1 html php jquery symfony twig

我正在使用Symfony2开发一个社交网络网站.实际上,我想知道如何在Symfony2中使用jquery/Ajax从PHP文件中获取数组字段值.在我的项目文件夹中的文件中有两个文件:test.html.twig和moslem1.php.这两个文件的代码如下:

test.html.twig的代码:

 <html>
    <head>
    </head>
    <body>
        <script src="{{asset('bundles/moslemtest/src/jquery.min.js')}}" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.ajax({
                    url: '{{asset('bundles/moslemtest/phptest/moslem1.php')}}',
                    type: 'POST',
                    dataType: 'JSON',
                    success: function(data1) {
                        var id=data1.id;
                        document.write(id);
                    }
                });
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

moslem1.php的代码:

    <?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM jqcalendar ORDER BY id DESC LIMIT 0, 1");
while($row = mysqli_fetch_array($result)) {
    $notification = array('id'=>$row['Id'],'subject'=>$row['Subject'],'location'=>$row['Location'],'description'=>$row['Description'],'starttime'=>$row['StartTime'],'endtime'=>$row['EndTime']);
}
echo json_encode($notification);
mysqli_close($con);
?>
Run Code Online (Sandbox Code Playgroud)

问题是每当我运行test.html.twig文件时它会显示如下:

未定义

奇怪的是,当我将文件test.html.twig的代码放在一个html文件中时,它可以正常工作.实际上我创建了另外两个文件(不在Symfony2中),它们是t1.html和moslem1.php.他们的代码如下:

t1.html的代码:

    <html>
    <head>
    </head>
    <body>
        <script src="jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.ajax({
                    url: 'moslem1.php',
                    type: 'POST',
                    dataType: 'JSON',
                    success: function(data1) {
                        var id=data1.id;
                        document.write(id);
                    }
                });
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

moslem1.php的代码:

    <?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM jqcalendar ORDER BY id DESC LIMIT 0, 1");
while($row = mysqli_fetch_array($result)) {
    $notification = array('id'=>$row['Id'],'subject'=>$row['Subject'],'location'=>$row['Location'],'description'=>$row['Description'],'starttime'=>$row['StartTime'],'endtime'=>$row['EndTime']);
}
echo json_encode($notification);
mysqli_close($con);
?>
Run Code Online (Sandbox Code Playgroud)

正如我上面所说,它在我处理HTML文件时有效,但是当我在Symfony2上工作时,我必须使用带有"html.twig"扩展名的文件来进行视图(输出).所以我的问题是我该怎么做才能使它与我的twig文件一起正常工作?

提前致谢.

vob*_*nce 6

将php文件放入bundles文件夹是一个非常糟糕的主意,这是一个巨大的安全问题.如果您想要AJAX响应,请将其放入Controller中.

像往常一样在Controller中创建一个新操作.编写AJAX操作并JSONResponse作为响应返回.

/**
* Return an ajax response
*/
public function ajaxAction(){
   // do the controller logic
   // $response is an array()
   return new JSONResponse($response);
}
Run Code Online (Sandbox Code Playgroud)

然后在jQuery ajax中设置这样的url:

$.ajax({
  url: 'url/to/your/controller', // If you use the FOSJsRoutingBundle then it will be Routing.generate('route_name')
  type: 'POST',
  dataType: 'JSON',
  success: function(data1) {
    var id=data1.id;
    document.write(id);
  }
});
Run Code Online (Sandbox Code Playgroud)

在新操作中,您可以使用它EntityManager来访问您的数据库.

这是一篇关于这个问题的好文章:http://symfony2forum.org/threads/5-Using-Symfony2-jQuery-and-Ajax

您还应该考虑使用FOSJsRoutingBundlefor javascript路由.它是一个非常棒的包,允许您在javascript文件中生成路由,因为您可以使用该path('path_name')命令在twig中执行此操作.

以下是此捆绑包的GitHub项目:https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

尝试了解Symfony请求 - >响应过程,因为如果您不遵循它,将导致严重的问题.