我正在尝试将存储库对象作为 json 发送
我的 Controller 代码:
if($request->isXmlHttpRequest()){
$data = $request->request->get('id');
$bedroom = $em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id'=>$data));
$this->container->get('logger')->addInfo('somesh');
return $json = new Response(json_encode(array('bedroom' => $bedroom)));
}
我的 JavaScript 代码:
$(document).ready(function(){
var id_select = $('#bedroom_view_bedroom').val();
$.ajax({
type: 'POST',
url: '{{ (path('listingview', {'id': id})) }}',
contentType: 'application/x-www-form-urlencoded',
data: {id:id_select},
success: function(result,status,xhr){
var inst_arr = JSON.parse(result);
console.log(inst_arr);
},
error: function(xhr, status, error) {
console.log(status);
}
});
});
这里我得到空对象作为响应。我如何使用 symfony3 将存储库对象作为 json 发送。帮助我任何人。提前致谢...
编辑:
我不知道哪里出了问题,我尝试了下面的代码,例如
if ($request->isXmlHttpRequest()) {
$data = $request->request->get('id');
$bedroom = $em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id' => $data));
if (null === $bedroom) {
$bedroomJson = '';
}
$this->container->get('logger')->addInfo('somesh');
$serializer = $this->container->get('serializer');
$bedroomJson = $serializer->serialize($bedroom, 'json');
return new Response($bedroomJson, Response::HTTP_OK, ['content-type' => 'application/json']);
}
控制台显示
jquery.min.js:4 POST http://localhost/epitahousing/web/app_dev.php/provider/listing/view/8 500 (Internal Server Error)error
如果我尝试在 xmlHttpRequest 之前找到存储库对象,例如
$bedroom = $em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id' => '12'));
if ($request->isXmlHttpRequest()) {
//code
}
这里显示了正确的对象,但我真的不知道 json 出了什么问题
编辑2:
$bedroom = $this->em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id'=>$data));
$output['rentamount'] = $bedroom->getRentamount();
$output['rentcurrency'] = $bedroom->getRentcurrency()->getValue();
$output['rentduration'] = $bedroom->getRentduration()->getValue();
$output['bondamount'] = $bedroom->getBondamount();
$output['bondcurrency'] = $bedroom->getBondcurrency()->getValue();
$output['leaseminduration'] = $bedroom->getLeaseminduration()->getValue();
$output['leasemaxduration'] = $bedroom->getLeasemaxduration()->getValue();
return $json = new Response(json_encode($output));
我这样做了,现在效果很好...... 感谢大家...如此有值(value)的回复
最佳答案
Symfony 附带 Serializer Component (选择您的 Sf 版本),您应该使用它来将对象转换为 json。
app/config/config.yml
启用序列化器组件
serializer:
enabled: true
您的 Controller
if($request->isXmlHttpRequest()){
$data = $request->request->get('id');
$bedroom = $em->getRepository('EpitaHousingBundle:Bedroom')->findOneBy(array('id'=>$data));
if(null === $bedroom) {
// return some error response
}
$this->container->get('logger')->addInfo('somesh');
$serializer = $this->container->get('serializer');
$bedroomJson = $serializer->serialize($bedroom, 'json');
return new Response($bedroomJson, Response::HTTP_OK, ['content-type' => 'application/json']);
}
$bedroomJson
应该是代表实体对象的 json 字符串。如果有问题请告诉我:)希望有帮助!
关于javascript - 如何在 Symfony 3 中以 Json 形式从存储库返回对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47386357/