2009-05-17 82 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只會在響應的正確內容類型的情況下提供它。