[英]Javscript returns [object Object]
I have the following PHP code on countries.php
.我对下面的PHP代码
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
并在
onboard.js
关注 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]
.但是,此处 jquery 代码中的
countriesList
返回[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.上面给出的数据是在切换到
RAW
格式后获取的。
var questions = [{
question: "What's your country?",
type: "select",
response: countriesList.map(function(elem) {
return elem.name;
})
// response: countriesList.map(function(elem) {
// return '<option value="'+elem.name+'">'+elem.name+'</option>';
// }).join("")
}]
Try one of the following尝试以下方法之一
alert(JSON.stringify(countriesList)); //or console.log(countriesList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.