2009-05-17 84 views
7

我正在尝试创建一个小的ajax聊天系统(仅用于它),我使用prototype.js来处理ajax部分。在Ajax响应回调中获取json

我在帮助中读到的一件事是,如果您返回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。

任何人都知道我可以做这个工作吗?

回答

22

这是检索JSON with Prototype

onSuccess: function(response){ 
    var json = response.responseText.evalJSON(); 
} 
+0

谢谢! 但我确实读过关于第二个参数的地方:P – AntonioCS 2009-05-17 22:49:32

+0

谢谢Jose。是的,http://www.prototypejs.org/learn/introduction-to-ajax它说第二个参数是json,废话,不会为我工作 - onSuccess:function(transport,json){alert(json?Object。检查(json):“没有JSON对象”); } – umpirsky 2010-10-12 10:04:05

1

你也可以只跳过框架正确的语法。这里是做AJAX一个跨浏览器兼容的方式,在一个评论插件使用:

 
//fetches comments from the server 
CommentWidget.prototype.getComments = function() { 
    var commentURL = this.getCommentsURL + this.obj.type + '/' + this.obj.id; 
    this.asyncRequest('GET', commentURL, null); 
} 


//initiates an XHR request 
CommentWidget.prototype.asyncRequest = function(method, uri, form) { 
    var o = createXhrObject() 
    if(!o) { return null; } 
    o.open(method, uri, true); 
    o.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); 
    var self = this; 
    o.onreadystatechange = function() {self.callback(o)}; 
    if (form) { 
    o.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
    o.send(makePostData(form)); 
    } else { 
    o.send(''); 
    } 
} 

//after a comment is posted, this rewrites the comments on the page 
CommentWidget.prototype.callback = function(o) {     
    if (o.readyState != 4) { return } 
    //turns the JSON string into a JavaScript object. 
    var response_obj = eval('(' + o.responseText + ')'); 
    this.comments = response_obj.comments; 
    this.refresh() 
} 

我开源这里这段代码http://www.trailbehind.com/comment_widget

2

这来自原型官方:

评估JavaScript响应 有时应用程序被设计为发送JavaScript代码作为响应 。 如果响应 的内容类型匹配MIME类型的JavaScript 那么这是真的,并且Prototype会自动eval()返回代码 。 如果您不需要,您不需要明确处理响应 。

替代地,如果响应持有 X-JSON头,其内容将是 解析,保存为一个对象,并发送至 回调作为第二个参数:

新的Ajax.Request('/ SOME_URL”,{ 方法: '得到',的onSuccess: 功能(运输,JSON){

alert(json ? Object.inspect(json) : "no JSON object"); 

} 

});

当您想用Ajax获取非平凡的 数据但希望避免解析XML响应的开销时使用此功能。 JSON比 XML快得多(也比较轻)。

3

有响应的属性:填充有仅如果后端返回的Content-Type一个JSON对象Response.responseJSON:应用/ JSON,即如果你做这样的事情在你的后端代码:在这种情况下

$this->output->set_content_type('application/json'); 
$this->output->set_output(json_encode($answer)); 
//this is within a Codeigniter controller 

Response.responseJSON =不确定,你可以检查在接收端,在你的onSuccess(T)处理:

onSuccess:function(t) { 
    if (t.responseJSON != undefined) 
    { 
    // backend sent some JSON content (maybe with error messages?) 
    } 
    else 
    { 
    // backend sent some text/html, let's say content for my target DIV 
    } 
} 

我没有真正回答有关处理程序的第二个参数的问题,但是如果它确实存在,那么Prototype只会在响应的正确内容类型的情况下提供它。