问题
我正在尝试获得AJAX响应,因此我可以摆弄它以使表单更易于使用。当我使控制器(下面的代码)使用返回正常响应时var_dump(),我得到了对象的输出,因此我知道查询没有错(我使用ID
1进行查询调试)。但是,当我使用返回输出时json_encode(),我只会得到一个空的JSON文件。
视图中的HTML表单
Write your name here:
同一视图中的脚本
$(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;
});
});
控制器响应正常(工作)
public function ajaxAction()
{
$location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')
->find(1);
$output = var_dump($location);
return $output;
}
具有AJAX响应的控制器(无效,返回空JSON)
public function ajaxAction()
{
$location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location')
->find(1);
return new Response(json_encode($location), 200);
}
有人可以帮我吗?这真让我发疯!