nasri_thamer
nasri_thamer

Reputation: 109

How can i each a Json response with Jquery?

Good morning , I am programming with Symfony . This is my Action :

public function selectcarsmonitorsAction($date) {
    $cars = array();
    $users= array();

    // Liste des voitures 
    $cars = $em->getRepository('ParcAutoBundle:Car')->FindAllCarsNotINCalendar($location, $date);

    // Liste des moniteurs
    $users= $em->getRepository('AuthenticationBundle:User')->FindAllUsersNotINCalendar($location, $date);

    foreach ($cars as $car) {
        $cars[] = $car->getId() . " " . $car->getMark() . " " . $car->getRegistrationnumber();
    }

    foreach ($users as $user) {
        $monitors[] = $user->getId() . " " . $user->getFirstname() . " " . $monitor->getLastname();
    }

    $response = new JsonResponse();
    $response->setData(array('cars' => $cars, 'users' => $users));

    return $response;
}

First : I don't know what's the underlined? Strange enter image description here

Second : I want to put this in a select and option ( a Select for cars and a select for Users).

thank you for your helps .

EDIT 1 :

How can i each this with JQUERY ??

enter image description here

Upvotes: 0

Views: 66

Answers (2)

nasri_thamer
nasri_thamer

Reputation: 109

in the each i putted Json.parse() :

   $.each(JSON.parse(cars), function (i, v) {
      selectcars += '<option value=' + v.id + '>' + v.mark + ' ' + v.registrationnumber + '</option>';
   });
   $('#cars').append(selectcars);

Upvotes: 0

squarer
squarer

Reputation: 63

second:

var select = $('#selectTag');
$.ajax({
    url: 'your_api',
    success: function(data) {
        var cars = data.cars;
        $.each(cars, function(index, item) {
            select.append($('<option>'), {
                value: xxx,
                text: xxx
            }));
        });
    },
    error: function(xhr, status, err) {
        console.error(url, status, err.toString());
    }
});

Upvotes: 2

Related Questions