我目前正在尝试使用Ajax找到基于邮政编码的地址。
当我执行它时,它会返回意外的显示。该列表不是具有搜索表单的地址列表,而是重复多次,直到它返回错误。
任何人都可以提供任何澄清吗?
文件:index.html.twig
<div class="loading"></div>
{% include 'LesproduwebBundle:Adresse:index.html.twig' with {'entities':entities } %}
<script>
$(".loading").hide();
$("#form_recherche").submit(function(){
$(".loading").show();
var codePostal = $("#codepostal").val();
var DATA = 'codePostal=' + codePostal;
$.ajax({
type: "POST",
url: "{{ path('adresse_recherhe')}}",
data: DATA,
cache: false,
success: function(data){
$('#resultats_recherche').html(data);
$(".loading").hide();
}
});
return false;
});
</script>
控制器
public function rechercherAction()
{
$request = $this->container->get('request');
if($request->isXmlHttpRequest())
{
$codePostal = '';
$codePostal = $request->request->get('codePostal');
$em = $this->container->get('doctrine')->getEntityManager();
if($codePostal != '')
{
$qb = $em->createQueryBuilder();
$qb->select('a')
->from('LesproduwebBundle:Adresse', 'a')
->where("a.codePostal LIKE :codePostal")
->setParameter('codePostal', '%'.$codePostal.'%');
$query = $qb->getQuery();
$entities = $query->getResult();
}
else {
$adresse = $em->getRepository('LesproduwebBundle:Adresse')->findAll();
}
return $this->container->get('templating')->renderResponse('LesproduwebBundle:Adresse:index.html.twig', array(
'entities ' => $entities
));
}
else {
return $this->indexAction();
}