Ver Mensaje Individual
  #2 (permalink)  
Antiguo 18/07/2017, 19:09
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Error 500 provocado por jquery

Problema resuelto, resulta que me faltaba importar el response.php de Symfony para que andara bien.

Código PHP:
Ver original
  1. <?php
  2. namespace UserBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use \UserBundle\Entity\Task;
  7. use \UserBundle\Form\TaskType;
  8. class TaskController extends Controller
  9. {
  10.     ...........
  11.     public function customAction(Request $request) {
  12.         $idUser = $this->get('security.token_storage')->getToken()->getUser()->getId();        
  13.         $em = $this->getDoctrine()->getManager();
  14.         $dql = "SELECT t FROM UserBundle:Task t JOIN t.user u WHERE u.id = :idUser ORDER BY t.id DESC";        
  15.         $tasks = $em->createQuery($dql)->setParameter('idUser' , $idUser);        
  16.         $paginator = $this->get('knp_paginator');
  17.         $pagination = $paginator->paginate(
  18.             $tasks,
  19.             $request->query->getInt('page', 1),
  20.             3
  21.         );        
  22.         $updateForm = $this->createCustomForm(':TASK_ID', 'PUT', 'task_process');        
  23.         return $this->render('UserBundle:Task:custom.html.twig',[
  24.             'pagination' => $pagination,
  25.             'update_form' => $updateForm->createView()
  26.         ]);
  27.     }
  28.    
  29.     public function processAction($id, Request $request) {
  30.         $em = $this->getDoctrine()->getManager();
  31.         $task = $em->getRepository('UserBundle:Task')->find($id);        
  32.         if(!$task) {
  33.             throw $this>createNotFoundException('La tarea no existe');
  34.         }        
  35.         $form = $this->createCustomForm($task->getId(), 'PUT', 'task_process');
  36.         $form->handleRequest($request);        
  37.         if($form->isSubmitted() && $form->isValid()) {
  38.             $success = $this->get('translator')->trans('The task has been processed.');
  39.             $warning = $this->get('translator')->trans('The task has already been processed.');
  40.             if ($task->getStatus() == 0) {
  41.                 $task->setStatus(1);
  42.                 $em->flush();                
  43.                 if($request->isXMLHttpRequest()) {
  44.                     return new Response(
  45.                         json_encode(['processed' => 1, 'success' => $success]),
  46.                         200,
  47.                         array('Content-Type' => 'application/json')
  48.                     );
  49.                 }
  50.             } else {
  51.                 if($request->isXMLHttpRequest()) {
  52.                     return new Response(
  53.                         json_encode(['processed' => 0, 'warning' => $warning]),
  54.                         200,
  55.                         array('Content-Type' => 'application/json')
  56.                     );
  57.                 }            
  58.             }
  59.         }
  60.     }
  61. }

Pongo lo que se necesita del controlador para que ande lo de jquery por si alguien lo quiere.

Saludos