4

I am working on a Symfony2 project where I have a user entity and I need an ajax search bar to search for my users. The problem is that in my AJAX response the controller, for some reason, returns all users from the database.

js

$('#search').keyup(function() {

     searchText = $(this).val();

     $.ajax({
        type: "GET",
        url: "/Apana/web/app_dev.php/search",
        dataType: "json",
        data: {searchText : searchText},
        success : function(response) 
          {
                console.log(response);
          }
    });
});

The controller

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;

use Apana\Bundle\MainBundle\Entity\User;

class SearchController extends Controller
{

    public function liveSearchAction(Request $request)
    {

        $string = $this->getRequest()->request->get('searchText');
        //$string = "alfa";
        $users = $this->getDoctrine()
                     ->getRepository('ApanaMainBundle:User')
                     ->findByLetters($string);

        //return users on json format

        $encoders = array(new XmlEncoder(), new JsonEncoder());
        $normalizers = array(new GetSetMethodNormalizer());
        $serializer = new Serializer($normalizers, $encoders);

        $jsonContent = $serializer->serialize($users, 'json');

        $response = new Response($jsonContent);
        return $response;
    }
}

User Repository

    class UserRepository extends EntityRepository
{

    public function findByLetters($string){
        return $this->getEntityManager()->createQuery('SELECT u FROM ApanaMainBundle:User u  
                WHERE u.firstname LIKE :string OR u.lastname LIKE :string')
                ->setParameter('string','%'.$string.'%')
                ->getResult();
    }
}

If I give a static text for my string parameter and visit the route for the controller, it works fine.

Tom Tom
  • 3,680
  • 5
  • 35
  • 40
Achilles
  • 114
  • 4
  • 11
  • 1
    Did you check that the value is getting passed to javascript ? You might also want to try with `type: "POST"`and try sending back the string for debug. – Tom Tom Jul 23 '14 at 23:28
  • Strange, just changed the type to "POST" ... and works perfect !!! – Achilles Jul 24 '14 at 00:33

1 Answers1

5

So the answer was to change the following:

     $.ajax({
        type: "GET",
        [...]

To

     $.ajax({
        type: "POST",
        [...]

It is not that strange you just probably weren't actually getting the GET parameter: How to get the request parameters in symfony2

public function updateAction(Request $request)
{
    // $_GET parameters
    $request->query->get('name');

    // $_POST parameters
    $request->request->get('name');

And there are a couple other possible methods mentioned there. It would have solved your problem too but it is always better not to use GET if you don't have to.

Community
  • 1
  • 1
Tom Tom
  • 3,680
  • 5
  • 35
  • 40