【问题标题】:undefined data result ajax symfony未定义的数据结果 ajax symfony
【发布时间】:2023-03-17 10:46:01
【问题描述】:

当我搜索产品时为我显示未定义的 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);
}

【问题讨论】:

  • return new JsonResponse($products) 这样它会为您编码响应。除此之外,undefined 不会来自 Symfony。这听起来更像 Javascript,那么首先调用 /searchProduit 是什么?

标签: php ajax symfony controller symfony4


【解决方案1】:

首先检查,如果您的 $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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 2014-03-01
    • 1970-01-01
    • 2013-01-09
    • 2016-10-05
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    相关资源
    最近更新 更多