Jquery 条令收集Json响应问题
因此,我有和Ajax POST,他们在控制器中调用一个函数,重新调用json响应,如下所示:Jquery 条令收集Json响应问题,jquery,json,ajax,symfony,doctrine-orm,Jquery,Json,Ajax,Symfony,Doctrine Orm,因此,我有和Ajax POST,他们在控制器中调用一个函数,重新调用json响应,如下所示: $images = $this->getEntityManager()->getRepository('AppBundle:Image')->findBy(array(),array(),5); $response = new JsonResponse(); $response->setData(array( 'images' =>
$images = $this->getEntityManager()->getRepository('AppBundle:Image')->findBy(array(),array(),5);
$response = new JsonResponse();
$response->setData(array(
'images' => $images
));
return $response;
我有五种元素,但都是空的。。。如下图所示:
例如,我从数组中的元素i返回一些属性,它可以工作:
$images = $this->getEntityManager()->getRepository('AppBundle:Image')->findBy(array(),array(),5);
$response = new JsonResponse();
$response->setData(array(
'images' => $images[0]->getSlug()
));
return $response;
以下是简单的ajax代码:
$.ajax({
method: 'POST',
url: $("#scroll-down").attr("data-href"),
success: function(response) {
console.log(response);
},
error: function() {
console.log(response);
}
}).fail(function() {
console.log(response);
});
在Symfony
JsonResponse
中,它主要是php函数的包装器json\u encode
json_encode
不会对私有对象属性进行编码
<?php
class Test {
private $a = null;
public $b = 3;
public function __construct($a) {
$this->a = $a;
}
}
$test = new Test(1);
echo json_encode($test);
对,条令ArrayCollection有一个名为$elemnts的私有属性。解决方案可以是使用适当的库来序列化对象,比如jms\u序列化程序。这个库使用反射,然后私有方法也将被序列化!