Symfony2 在 AJAX 调用上返回空的 JSON 而变量不为空

2022-01-03 00:00:00 mysql symfony ajax doctrine-orm

问题

我正在尝试获得 AJAX 响应,以便我可以摆弄它以使我的表单更易于使用.当我让控制器(下面的代码)返回一个带有 var_dump() 的正常响应时,我得到了对象的输出,所以我知道查询没有错(我使用 ID 1 查询以进行调试).但是,当我使用 json_encode() 返回输出时,我只会得到一个空的 JSON 文件.

视图中的 HTML 表单

<form id="myForm" action="{{path('snow_ajax')}}" method="POST" >在这里写下你的名字:<input type="text" name="name" id="name_id" value=""/><br/><输入类型=提交"值=发送"/></表单>

同一视图中的脚本

控制器正常响应(工作)

公共函数ajaxAction(){$location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')->查找(1);$output = var_dump($location);返回 $output;}

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

公共函数ajaxAction(){$location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')->查找(1);返回新响应(json_encode($location), 200);}

有人可以帮我吗?这让我发疯!

解决方案

我设法通过使用 Doctrine2 的实体管理器将结果放入数组中来修复它,然后我继续将其编码为 JSON.我不确定这是否是最干净的方法(根据我的 IDE,getEntityManager() 似乎已被弃用)但它现在工作正常.

公共函数ajaxAction(){$em = $this->getDoctrine()->getEntityManager();$query = $em->createQuery('SELECT l FROM SnowFrontBundleEntityLocation l WHERE l.id=:id');$query->setParameter('id', 1);$result = $query->getArrayResult();返回新响应(json_encode($result), 200);}

Problem

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.

HTML form in the view

<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;
}

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!

解决方案

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 SnowFrontBundleEntityLocation l WHERE l.id=:id');
    $query->setParameter('id', 1);
    $result = $query->getArrayResult();

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

相关文章