我的代码中有一个ajax调用。我想通过通话实现的效果很好。我想从数据库中删除一些记录,当通过ajax调用该方法时,该记录实际上被删除了,但是就像在symfony方法中,它必须返回一个响应,这就是为什么当执行该方法时会给我错误
我的Ajax电话是
$.ajax({ type: "POST", data: data, url:"{{ path('v2_pm_patents_trashpatents') }}", cache: false, success: function(){ document.location.reload(true); } });
而执行的方法是
public function trashpatentAction(Request $request){ if ($request->isXmlHttpRequest()) { $id = $request->get("pid"); $em = $this->getDoctrine()->getEntityManager(); $patent_group = $em->getRepository('MunichInnovationGroupPatentBundle:PmPatentgroups')->find($id); if($patent_group){ $patentgroup_id = $patent_group->getId(); $em = $this->getDoctrine()->getEntityManager(); $patents = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents') ->findBy(array('patentgroup' => $patentgroup_id)); if($patents){ foreach($patents as $patent){ if($patent->getIs_deleted()==false){ $patent->setIs_deleted(true); $em->flush(); } } } $patent_group->setIs_deleted(true); $em->flush(); } else{ $em = $this->getDoctrine()->getEntityManager(); $patent = $em->getRepository('MunichInnovationGroupPatentBundle:SvPatents')->find($id); if ($patent) { $patent->setIs_deleted(1); $em->flush(); } } return true; } }
我如何从该方法成功返回?有任何想法吗?谢谢
替换return true;为return new Response();。也不要忘记use Symfony\Component\HttpFoundation\Response;在顶部写。
return true;
return new Response();
use Symfony\Component\HttpFoundation\Response;