13

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

My Ajax call is

  $.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

j0k
  • 22,600
  • 28
  • 79
  • 90
Zoha Ali Khan
  • 1,659
  • 11
  • 37
  • 56
  • @Stony does it will effect if we use annotation?Because I use YML it's easy and understandable. – Azam Alvi Mar 28 '14 at 11:53
  • Checking this is not good, if ($request->isXmlHttpRequest()), suppose you want to test in another tab of the browser to check that your response is right (ignoring a misconfigured option in your JS request that can cause a bad response behavior), you can't... Every controller must always give you a response (200, 204, 400, etc...) Your controller ignores the request if not is an AJAX request... – Tecnocat Aug 06 '17 at 09:42

2 Answers2

56

Replace return true; with return new Response();. Also don't forget to write use Symfony\Component\HttpFoundation\Response; at the top.

Anton Babenko
  • 6,586
  • 2
  • 36
  • 44
3

You can also pass error code 200 and the content type like below.

return new Response('Its coming 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'));
    }
}
Dawid D
  • 1,057
  • 13
  • 28
Sam Kaz
  • 432
  • 3
  • 12