从我的数据库中我获取了一些数据作为json string.But遗憾的是我无法访问作为响应返回的数据。以下是我获取数据的php页面:
require_once '../core/init.php';
$answer=$_POST['answer_body'];
$post_id=$_POST['userpost_post_id'];
$answerer=$_POST['users_user_id'];
if(isset($answer,$post_id,$answerer)){
if(!empty($answer) && !empty($post_id) && !empty($answerer)){
$db=DB::getInstance();
if($result=$db->post_and_fetch("CALL login.post_and_fetch_ans(?,?,?)",array($answer,$post_id,$answerer))->result()){
echo json_encode($result);
}else{
echo 'there was a problem';
}
}
}
它返回如下:
$.ajax('../includes/verifyanswer.php',{
data:data,
type:"POST",
datatype:'json',
success:function(response){
alert(response['answer_body']); // prints undefined
},
error:function(response){
alert(response);
}
})
答案 0 :(得分:0)
您的response
是一个字符串,而不是一个数组。您应该使用getJSON()
来确保将响应解析为对象:
$.getJSON(
'../includes/verifyanswer.php',
data,
function(response) {
alert(response.answer_body);
}
);