代码之家  ›  专栏  ›  技术社区  ›  AntonioCS

在Ajax响应回调中获取json

  •  7
  • AntonioCS  · 技术社区  · 16 年前

    我正在尝试创建一个小的ajax聊天系统(只是为了好玩),我正在使用prototype.js来处理ajax部分。

    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));
    

    onSuccess: function (response,json) {
                    alert(response.responseText);
                    alert(json);    
                }
    

    4 回复  |  直到 14 年前
        1
  •  22
  •   Jose Basilio    16 年前

    这是检索的正确语法 JSON with Prototype

    onSuccess: function(response){
       var json = response.responseText.evalJSON();
    }
    
        2
  •  3
  •   Yuri Makassiouk    13 年前

    Response:responseJSON有一个属性,只有当后端返回Content-Type:application/JSON时,它才会填充JSON对象,也就是说,如果你在后端代码中做了这样的事情:

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

    在这种情况下,Response.responseJSON!=undefined,您可以在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
      }
    }
    

        3
  •  2
  •   armel    15 年前

    这来自Prototype官方:

    评估JavaScript响应 有时应用程序是设计的 发送JavaScript代码作为响应。 那么这是真的,Prototype将

    或者,如果响应成立

    新Ajax。请求(“/some_url”{ 方法:“get”,on成功: 函数(传输,json){

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

    使用Ajax处理数据,但希望避免 XML。

        4
  •  1
  •   Andrew Johnson    16 年前

    //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

    推荐文章