如何解决未定义的数据结果 ajax symfony
搜索产品时未定义的ajax显示
/**
* @Route("/searchProduit ",name="searchProduit")
*/
public function searchProduit(Request $request,NormalizerInterface $Normalizer)
{
$repository = $this->getDoctrine()->getRepository(Produit::class);
$requestString=$request->get('searchValue');
$products = $repository->findProduitByName($requestString);
$jsonContent = $Normalizer->normalize($products,'json',['groups'=>'products']);
$retour = json_encode($jsonContent);
return new Response($retour);
}
解决方法
首先检查,如果您的 $products
返回任何内容,例如倾倒它:dump($products);
如果是,尝试返回 JsonResponse
use Symfony\Component\HttpFoundation\JsonResponse;
// encode your $products on the fly
$response = new JsonResponse($products);
// if the data to send is already encoded in JSON
$response = JsonResponse::fromJsonString('$jsonContent');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。