Symfony2在AJAX调用时返回空的JSON,而变量不为空 [英] Symfony2 returns empty JSON on AJAX call while variable isn't empty

查看:77
本文介绍了Symfony2在AJAX调用时返回空的JSON,而变量不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得一个AJAX响应,所以我可以围绕它来使我的表单更容易使用。当我使控制器(下面的代码)返回一个正常的回应与 var_dump(),我得到对象的输出,所以我知道查询没有错(我正在使用ID 1进行查询以进行调试)。但是,当我以 json_encode()返回输出时,我只需要一个空的JSON文件。

I'm trying to get an AJAX response so I can fiddle around with it to make my forms easier to use. When I make the controller (code below) return a normal response with var_dump(), I get the object's output so I know the query isn't wrong (I'm using ID 1 to query to debug). However, when I return the output with json_encode(), I just get an empty JSON file.

<div id="content">
    <form id="myForm" action="{{path('snow_ajax')}}" method="POST" >
        Write your name here:
        <input type="text" name="name" id="name_id" value="" /><br />
        <input type="submit" value="Send" />
    </form>
</div>



同一视图中的脚本



Script in the same view

<script type="text/javascript">
    $(document).ready(function() {

        $("#myForm").submit(function(){
            var url=$("#myForm").attr("action");

            $.post(url,{
                formName:"ajaxtest",
                other:"attributes"
            },function(data){

                if(data.responseCode==200 ){
                    alert("Got your json!");
                }
                else{
                    alert("something went wrong :(");
                }
            });
            return false;
        });
    });
</script>



具有正常响应(作品)的控制器



Controller with normal response (works)

public function ajaxAction()
{

    $location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')
        ->find(1);

    $output = var_dump($location);

    return $output;
}



具有AJAX响应的控制器(不起作用,返回空的JSON)



Controller with AJAX response (doesn't work, returns empty JSON)

public function ajaxAction()
{

    $location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')
        ->find(1);

    return new Response(json_encode($location), 200);
}

请问有人可以帮我吗?这是驱动我的坚果!

Could anyone help me out here, please? This is driving me nuts!

推荐答案

我设法通过使用Doctrine2的实体管理器来获取结果在数组中,我继续把它编码成JSON。我不知道这是否是最干净的方法(getEntityManager()似乎根据我的IDE被弃用),但现在可以正常工作。

I managed to fix it by using Doctrine2's entity manager to get the result in an array, after which I proceeded to encode it into JSON. I'm not sure if this is the cleanest way to do it (getEntityManager() seems to be deprecated according to my IDE) but it works fine for now.

public function ajaxAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $query = $em->createQuery('SELECT l FROM Snow\FrontBundle\Entity\Location l WHERE l.id=:id');
    $query->setParameter('id', 1);
    $result = $query->getArrayResult();

    return new Response(json_encode($result), 200);
}

这篇关于Symfony2在AJAX调用时返回空的JSON,而变量不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆