Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to fetch categories list from my database and put it in my javascript code so i can use it later. But I've encountered problems with this task - after returning this list to javascript - they are empty.

Here is my symfony2 controller action code:

public function fetchCategoriesAction(){
     $categories = $this->getDoctrine()->getRepository('MyDataBundle:Category')->findAll();
     $return=array("responseCode"=>200,  "categories"=>$categories);
     $return=json_encode($return);//jscon encode the array
     return new Response($return,200,array('Content-Type'=>'application/json'));
}

Here is my js code:

var categories;

function categoriesLoad(){

var url=$("#categories_fetch").val(); //link to my controller

$.post(url,function(data){

     if(data.responseCode==200 ){           
         categories = data.categories;
         console.log(categories);
     }else{
       console.log("An unexpeded error occured.");
    }
});

}

I'm running $(document).ready(function() { categoriesLoad(); });

But then after using console.log(categories) I'm getting empty objects, although their number match the number of records in the database.

I'm just starting programming in symfony2 and I'd appreciate any help :)

EDIT:

SOLVED

I've just changed my controller action code. Here it is updated:

public function fetchCategoriesAction(){

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

    $em = $this->getDoctrine()->getManager();
    $categories = $em->createQuery(
            'SELECT u
            FROM MyDataBundle:Category u'
    )->getResult();

    $categories = $serializer->serialize($categories, 'json');
    $return=array("responseCode"=>200,  "categories"=>$categories);
    $return=json_encode($return);
    return new Response($return,200,array('Content-Type'=>'application/json'));
}

Now it works fine. Thanks to @Pawel and @SAM

share|improve this question
 
Few tips, 1. Are you getting the $categories array in php side, 2. if you are getting the value for $categories, then try to console.log(data); 3. define the categories : var categories = data.categories; Let me know the result –  SAM Aug 1 '13 at 13:16
 
@SAM 1. I've made test function before at php side and it contains variables it looks like this : public function testAction(){ $categories = $this->getDoctrine()->getRepository('VelrezDataBundle:Category')->findAll(); return new Response(var_dump( $categories )); } –  Tomasz Grochowski Aug 1 '13 at 13:22
 
can you tell me the status of 2. if you are getting the value for $categories, then try to console.log(data); So that i can get some clue by any chance –  SAM Aug 1 '13 at 13:24
 
the output is like this:array(5) { [0]=> object(Proj\DataBundle\Entity\Category)#333 (2) { ["name":"Proj\DataBundle\Entity\Category":private]=> string(8) "Test2" ["id":"Proj\DataBundle\Entity\Category":private]=> int(17) } [1]=> object(Proj\DataBundle\Entity\Category)#334 (2) { ["name":"Proj\DataBundle\Entity\Category":private]=> string(10) "Multimedia" ["id":"Proj\DataBundle\Entity\Category":private]=> int(15) }... } –  Tomasz Grochowski Aug 1 '13 at 13:25
1  
Refer this: stackoverflow.com/questions/4697656/… –  SAM Aug 1 '13 at 13:36
show 4 more comments

1 Answer

up vote 0 down vote accepted

My example: PHP function:

public function getDistrictListAction($id) {

        $em = $this->getDoctrine()->getManager();

        $query = $em->createQuery(
           // DQL
        );

        $json = json_encode( $query->getResult() );
        return new JsonResponse( $json );
    }

JS Code:

var dropdown = $('.dropdown-selector');
dropdown.change( function() {
    $.ajax({
         url: 'url_to_function'
         beforeSend: function() {
              dropdown.attr('disabled', true);
         },
         success: function(data) {
              dropdown.find('option').not('[value=]').remove();
              $.each( JSON.parse(data), function(key, value) {
                   dropdown.append( $( "<option></option>" ).attr( "value", value.id ).text( value.name ));
              });
              dropdown.attr('disabled', false);
         }
    });
}

This you can set on change event for example as a callback. Before send you make dropdown disabled, and after load via AJAX option you are enabale it again.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.