我正在尝试解析从DB返回并编码为JSON对象的信息。
这是检索信息的代码:
private function retrieve_standards_one(){
$dbh = $this->connect();
$stmt = $dbh->prepare("SELECT code, standard_one_id
FROM standard_one
WHERE grade_id = :grade_id
ORDER BY standard_one_id");
$stnd = array();
for($x = 0; $x < (count($this->grades)); $x++){
$stmt->bindParam(':grade_id', $this->grades[$x], PDO::PARAM_STR);
$stmt->execute();
$stnd[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
$json = json_encode($stnd);
return $json;
}
并且这个我正在尝试解析信息:
$.ajax({
type: "POST",
url: "lib/search/search.standards_one.php",
async: "false",
data: {subjects: subjects, grades: grades},
success: function(response){
$("#standards_results").html("");
var obj = $.parseJSON(response);
$.each(obj, function(){
alert(this['code'] + ", " + this['standard_one_id'])
});
}
});
我尝试了很多不同的方法,但我只是将[object] [object]作为回应。
这是回复:
答案 0 :(得分:2)
使用
console.log(this['code'] , this['standard_one_id'])
而不是
alert(this['code'] + ", " + this['standard_one_id'])
答案 1 :(得分:0)
将dataType属性添加到AJAX调用中。
$ .ajax({
type: "POST",
dataType: "JSON",
url: "lib/search/search.standards_one.php",
async: "false",
data: {subjects: subjects, grades: grades},
success: function(response){
$("#standards_results").html("");
var obj = $.parseJSON(response);
$.each(obj, function(){
alert(this['code'] + ", " + this['standard_one_id'])
});
}
});