2
votes

I am calling a Symfony action via jQuery AJAX, however one of the actions is returning 404 and I am not sure why.

The Scenario.

An online system hosts a list of projects, these project can be added to a local system.

The user searches by Id and and if a project with that Id exists and they have not already added it the project will be added to their local system.

javascript

 //CHECK IF EXTERNAL PROJECT CAN BE IMPORTED
 $.get("/find-project/" + Id, function( data ) { << THIS ONE IS GOOD

 if(data['status'] === false){
 //PROJECT WITH THAT ID NOT FOUND
 }else{
 //PROJECT WITH THAT ID EXISTS


// HAVE YOU ALREADY ADDED THAT PROJECT
$.get("/check-project/" + Id, function(data){ << THIS RETURNS 404

if(data === false){
 //PROJECT HAS NOT BEEN ADDED.. ADD IT

 }else{
  //PROJECT HAS ALREADY BEEN ADDED
  }
  })
  }
   });

Symfony Controller PHP

/**
 * @Route("/find-project/{Id}", name="_findProject")
 * @Method({"GET"})
 */
public function findProjectByIdAction($Id)
{
    $project = $this->get('crm')->findProjectById($Id);
    $status = $project ? true : false;

    return new JsonResponse(array('status' => $status, 'project' => $project));
}



/**
 * @Route("/check-project/{Id}", name="_checkProject")
 * @Method({"GET"})
 */

//THIS FUNCTION CANNOT BE FOUND 404

public function checkIfProjectExistsAction($Id){

    $query = $this->getDoctrine()->getRepository('AppBundle:Project')->findOneBy(array('Id' => $Id));
    $cnt = count($query->getResult());

    $cnt == 0 ? $status = false : $status = true ;

    return new JsonResponse(array('status' => $status));
}

Already checked; Site is an available as route from route:debug in console

The requests return as

Remote Address:127.0.0.1:443 Request URL:https://localhost/check-project/12345 Request Method:GET Status Code:404 Not Found

Any help greatly appreciated

1
Since you are using the production environment have you cleared the cache to update it with the current routes? Try localhost/app_dev.php/check-project/12345dk80
Shouldn't you ->getManager() before you ->getRepository()?D4V1D
1. Clear cached on the production environment rm -rf app/cache/* 2. Check the scope of Id variable in javascript maybe it's local and you are sending ajax call with undefinedAlexandru Olaru
thank you very much guys it turned out to be a caching issue.jmack

1 Answers

0
votes

->findOneBy(array('Id' => $Id));

are you sure the 'Id' key is correct ?

BTW. Returning true or false is ugly, you should throw not found exception if no records are present