I have the following PHP code on countries.php
.
$stmt = $pdo->prepare("SELECT id, name FROM countries");
$stmt-> execute();
$data = array();
while($row = $stmt->fetch()){
$data[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
echo json_encode($data);
And following JS in onboard.js
$.ajax({
url: "processes/countries.php",
type: 'GET',
dataType: 'json',
success: function(countriesList) {
var questions = [
{
question: "What's your country?",
type: "select",
// response : ['India', 'America', 'Japan']
response: $.each(countriesList, function(){
this.name; // DISPLAYS [object Object]
})
}
]
}
});
As you can see I am trying to get the list of countries and display it. However, countriesList
in jquery code here returns [object Object]
. How can I get the response to print the countries names?
Network tab response shows data is returned in the following manner:
[
{"id":"1","name":"Afghanistan"},
{"id":"2","name":"Albania"},
{"id":"3","name":"Algeria"},
{"id":"4","name":"American Samoa"},
]
The data given above is taken after switching to the RAW
format.