ajax 控制器必须在 Symfony2 中返回响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11190770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controller must return a response in Symfony2
提问by Zoha Ali Khan
I have an ajax call in my code. What I want to achieve with the call works fine. I want to delete some records from the database which is actually deleted when the method is called via ajax but as in symfony method it must return a response and thats why when the method is executed its gives me the error
我的代码中有一个 ajax 调用。我想通过通话实现的效果很好。我想从数据库中删除一些记录,这些记录在通过 ajax 调用该方法时实际上已删除,但在 symfony 方法中,它必须返回一个响应,这就是为什么执行该方法时它给了我错误
My Ajax call is
我的 Ajax 调用是
$.ajax({
type: "POST",
data: data,
url:"{{ path('v2_pm_patents_trashpatents') }}",
cache: false,
success: function(){
document.location.reload(true);
}
});
And the method that is executed is
并且执行的方法是
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;
}
}
How can I successfully return from this method ? Any ideas? Thanks
我怎样才能从这个方法成功返回?有任何想法吗?谢谢
回答by Anton Babenko
Replace return true;with return new Response();. Also don't forget to write use Symfony\Component\HttpFoundation\Response;at the top.
替换return true;为return new Response();。也不要忘记写use Symfony\Component\HttpFoundation\Response;在顶部。
回答by Sam Kaz
You can also pass error code 200 and the content type like below.
您还可以传递错误代码 200 和如下所示的内容类型。
return new Response('Its coming from here .', 200, array('Content-Type' => 'text/html'));
return new Response('Its come from here.', 200, array('Content-Type' => 'text/html'));
Here is the full example of the controller
这是控制器的完整示例
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class WelcomeController extends Controller
{
public function indexAction()
{
return new Response('Its coming from here .', 200, array('Content-Type' => 'text/html'));
}
}
使用 Symfony\Bundle\FrameworkBundle\Controller\Controller;
使用 Symfony\Component\HttpFoundation\Response;
class WelcomeController extends Controller
{
public function indexAction()
{
return new Response('Its come from here.', 200, array('Content-Type' => 'text/html'));
}
}

