0

I have some javascript that used to call a PHP file to get a list of countries, using jquery.

The PHP returned a json encoded array that looked like this:

[ { "CountryID": "11", "CountryName": "Argentina" }, { "CountryID": "14", "CountryName": "Australia" }, { "CountryID": "15", "CountryName": "Austria" }, { "CountryID": "20", "CountryName": "Barbados" }, etc

My javascript read this OK and popped all the names into a list, like this:

var queryString = "api/countries.php";

$.get(queryString, function(data){

    var obj = JSON.parse(data); 
    var thisGet = obj.length;
    var countryList = document.getElementById('country_list');

    var opt = document.createElement('option');
    for (i = 0; i < thisGet; i++)
    {
        var opt = document.createElement('option');
        opt.value = obj[i].CountryID;
        opt.innerHTML = obj[i].CountryName;
        countryList.appendChild(opt);
    }
});

This worked fine, but I started using Silex (Symfony) for something else, so I created a route to get my countries. This works fine when you just type the URL into a browser or Postman, but it has to return a Response. This is the new code:

$app->get('/usedcountries', function() use($app) { 

    $result = getCountries();
    $response = new JsonResponse();
    $response->setData($result);
    return $response;
});

When my javascript gets the data now, it can't read it, and if I display it in an alert, I just get this:

[object Object],[object Object],... etc

Can anyone please explain what I need to do to get the data from the Response? I've looked at all the other questions on here that SO recommended, but none of them help.

(getCountries just does a call into a database, like this:

function getCountries() {
    global $DBH;

    $q = 'SELECT * FROM country_summary ORDER BY CountryName';
    $stmt = $DBH->prepare($q);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $rows = $stmt->fetchAll();
    return $rows;
}

TIA.

OriginalBigBri
  • 163
  • 2
  • 13
  • Don't use `alert` for development. `alert` converts the value to a **string**. Use `console.log` instead or set breakpoints. – Felix Kling Jun 23 '15 at 19:47
  • Perhaps you can point me to the part of that post (which I have read twice now) that answers my question? – OriginalBigBri Jun 23 '15 at 20:52
  • It looks like what you have should just work. The fact that you get that output means that you are receiving an array of objects. You might want to use `console.log` to verify the structure of the array. – Felix Kling Jun 23 '15 at 20:55
  • Thanks. It seems that I don't need to do this with the JsonResponse: var obj = JSON.parse(data); as the data is already in the right format. Also, I couldn't get it working without a root node, but when I added one I could access all the members from that. So, I now have this: for (i = 0; i < data.countries.length; i++) { var opt = document.createElement('option'); opt.value = data.countries[i].CountryID; opt.innerHTML = data.countries[i].CountryName; countryList.appendChild(opt); } – OriginalBigBri Jun 23 '15 at 21:40
  • Well, you should only use `console.log` for debugging your code, not use it in production. And yes, with the right headers, jQuery will parse the JSON automatically for you. I guess `JsonResponse` sets the right headers. You should have seen an error in the console actually. – Felix Kling Jun 23 '15 at 21:42
  • Ooop, I forgot you can't format code in a reply :( Thanks for your help. – OriginalBigBri Jun 23 '15 at 21:44

0 Answers0