获取json的Ajax响应回调

Ant*_*oCS 7 javascript php ajax prototypejs

我正在尝试创建一个小的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));
Run Code Online (Sandbox Code Playgroud)

在ajax请求我有:

onSuccess: function (response,json) {
                alert(response.responseText);
                alert(json);    
            }
Run Code Online (Sandbox Code Playgroud)

response.responseText的警报给了我{"lastid":8}但是json给了我null.

谁知道我怎么能做这个工作?

Jos*_*lio 22

这是使用Prototype检索JSON的正确语法

onSuccess: function(response){
   var json = response.responseText.evalJSON();
}
Run Code Online (Sandbox Code Playgroud)