Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t在聊天应用程序中使用Ajax调用从控制器返回html_Javascript_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Javascript Can';t在聊天应用程序中使用Ajax调用从控制器返回html

Javascript Can';t在聊天应用程序中使用Ajax调用从控制器返回html,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,因此,我正在CRM中构建一个集成的聊天应用程序。无论何时登录的用户单击联系人,它都会使用Ajax调用显示两个用户之间的聊天历史记录,这就是问题开始的地方 下面是我的Ajax调用代码: function GetChatHistory(receiver_id){ $.ajax({ dataType : "json", url: '/chat/get_chat_history_by_user', data:{r

因此,我正在CRM中构建一个集成的聊天应用程序。无论何时登录的用户单击联系人,它都会使用Ajax调用显示两个用户之间的聊天历史记录,这就是问题开始的地方

下面是我的Ajax调用代码:

function GetChatHistory(receiver_id){
    $.ajax({
              dataType : "json",
              url: '/chat/get_chat_history_by_user',
              data:{receiver_id:receiver_id},
              success:function(data)
              {

                  $('#chathistory').html(data);
                  ScrollDown();  
              },
              error: function (jqXHR, status, err) {
                  // alert('Local error callback');
                  alert("error fetching")
              }
         });
}
这是我的控制器

public function get_chat_history_by_user(){
        //get the receiver id
        $receiver_id = $this->input->get('receiver_id');
        //get the sender id
        $Logged_sender_id = $this->session->userdata['user_id'];

        $history = $this->chat_model->GetReciverChatHistory($receiver_id);

        foreach($history as $chat):

            $message_id = $chat['id'];
            $sender_id = $chat['sender_id'];
            $userName = $this->UserModel->GetName($chat['sender_id']);
            $userPic = $this->UserModel->PictureUrlById($chat['sender_id']);

            $messagebody = $chat['message'];
            $messagedatetime = date('d M H:i A',strtotime($chat['message_date_time']));
      ?>
           <?php if($Logged_sender_id!=$sender_id){?>     
                  <!-- Message. Default to the left -->
                    <div class="direct-chat-msg">
                      <div class="direct-chat-info clearfix">
                        <span ><?=$userName;?></span>
                        <span ><?=$messagedatetime;?></span>
                      </div>
                      <!-- /.direct-chat-info -->

                      <div class="direct-chat-text">
                         <?=$messageBody;?>
                      </div>
                      <!-- /.direct-chat-text -->

                    </div>
                    <!-- /.direct-chat-msg -->
            <?php }else{?>
               <!-- Message to the right -->
                    <div class="direct-chat-msg right">
                      <div class="direct-chat-info clearfix">
                        <span ><?=$userName;?></span>
                        <span ><?=$messagedatetime;?></span>
                      </div>
                      <!-- /.direct-chat-info -->

                      <div class="direct-chat-text">
                        <?=$messageBody;?>
                       </div>
                       <!-- /.direct-chat-text -->
                    </div>
                    <!-- /.direct-chat-msg -->
             <?php }?>

       <?php
        endforeach;
 }

然后控制台在我的ajax调用中记录数据,我可以毫无问题地获得完整的聊天历史记录。所以我猜foreach循环和html呈现有问题

另外:我在github上查看了一些简单的web应用聊天,他们也用同样的方式编写了控制器,对他们来说效果非常好。那么你认为问题出在哪里

dataType : "json" 
告诉jQuery在响应中使用JSON,但您正在从控制器返回HTML。因此,当它试图将HTML解析为JSON时,可能会抛出一个错误。请删除该行,或指定

dataType: "html" 

相反

您是否检查了浏览器控制台中的错误?或者执行任何其他基本调试步骤?如果你能说的最好的话是“我猜是出了什么问题”,那么我怀疑不是。调试几乎和编程本身一样重要,请至少学习如何做基础工作。无论如何,您几乎肯定会遇到一个控制台错误,因为
数据类型:“json”
告诉jQuery在响应中使用json,但是您正在从控制器返回HTML。因此,当它试图将HTML解析为JSON时,可能会抛出一个错误。请删除该行,或者指定
数据类型:“html”
。感谢您的反馈:)。你是对的,学习调试是很重要的,而我在这方面缺乏经验,我花时间在控制台上记录东西来解决问题。我会检查数据类型属性并让您知道!非常感谢你,伙计,这就是问题所在!我把它改成html,一切正常!
dataType : "json" 
dataType: "html"