我正在尝试创建一个小的 ajax 聊天系统(只是为了它),我正在使用 prototype.js 来处理 ajax 部分。
我在帮助中读到的一件事是,如果您返回 json 数据,回调函数将在第二个参数中填充该 json 数据。
所以在我调用的 php 文件中有:
header('Content-type: application/json');
if (($response = $acs_ajch_sql->postmsg($acs_ajch_msg,$acs_ajch_username,$acs_ajch_channel,$acs_ajch_ts_client)) === true)
echo json_encode(array('lastid' => $acs_ajch_sql->msgid));
else
echo json_encode(array('error' => $response));
在 ajax 请求中我有:
onSuccess: function (response,json) {
alert(response.responseText);
alert(json);
}
response.responseText 的警报给了我 {"lastid": 8 } 但 json 给了我 null。
任何人都知道我怎样才能使这个工作?
最佳答案
这是检索 JSON with Prototype 的正确语法
onSuccess: function(response){
var json = response.responseText.evalJSON();
}
关于php - 在 Ajax 响应回调中获取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/875630/