【问题标题】:Problem with JQuery getJSON retrieve data from Controller (Code igniter)JQuery getJSON 从控制器(Codeigniter)检索数据的问题
【发布时间】:2011-03-24 11:08:32
【问题描述】:


我的 JQuery、Ajax 和 PHP 代码有问题。 我有一个页面,其中包含来自用户的所有帖子。 然后在此页面中,我想使用 Ajax 加载该帖子的最新评论。 但是它没有显示任何内容。

这是我调用 JQuery 函数的 PHP 代码:

<? foreach($post_query as $post_row){ ?>
   <li class="post_list">
   <?php echo $post_row['text'];?>
   <br/>
    Latest Comment: 
    <br/>
    <script type="text/javascript">
        var post_id = $('#post_id_<?php echo $post_row['id'];?>').val();
        document.write(post_id);//just to check that post_id value is not undefined

        $(function(){
            $.getJSON("<?php echo base_url();?>postC/latest_comment", {post_id: post_id}, 
               function(res){
                  $("#result").prepend(res.text);
               });
        });

    </script>

<? } ?>

这是我处理请求的 PHP 代码点火器控制器:

class postC extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->model('models_facade');
        $this->load->Database();
        $this->load->helper('url');
        $this->load->helper('form');    
        $this->load->library('tank_auth');
        $this->load->library('user_session_lib');
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
    }
        function latest_comment(){
        $post_id        = $this->checkValues($_GET['post_id']);
        $latestcomment  =  $this->models_facade->getLatestCommentForPost($post_id);
        return json_encode($latestcomment);
    }
}

我已经测试过手动调用该函数并在不使用 ajax 的情况下显示最新评论,并且它有效。所以这意味着模型中从mysql检索结果的后端函数没有问题。
我也尝试过:

echo json_encode($latestcomment);  

尝试手动调用函数时,我可以看到数据是 JSON 格式。
所以这意味着,ajax 调用有问题,它没有从控制器获取数据。我对 .getJSON 使用 GET,对 .post() 和 .ajax() 使用 POST。

我已经尝试了所有版本的 jQuery Ajax 调用:.getJSON()、.post() 和 .ajax(),但都没有奏效。 我确信相应的 Jquery 库已经正确加载,因为我所有的其他 ajax 函数都可以正常工作。

有没有人知道错误可能在哪里?我已经调试了一整天,没有得到任何结果。
谢谢

【问题讨论】:

  • 使用调试器检测我是否成功执行了AJAX调用以及回复是什么。

标签: php json codeigniter jquery


【解决方案1】:

这就是我处理 jQuery Codeigniter 调用的方式,它对我有用。

在 jQuery 上试试这个:

$(function(){
        $.post("<?php echo base_url();?>postC/latest_comment", {post_id: post_id}, 
           function(data){
              $("#result").prepend(data.html);
           },'json');
    });

PHP:

function latest_comment(){
    $post_id        = $this->checkValues($_GET['post_id']);
    $jsonData['html']  =  $this->models_facade->getLatestCommentForPost($post_id);
    echo json_encode($jsonData);
}

这将允许您在返回的 JSON 对象中添加更多内容。我喜欢使用的一个技巧是向数组添加“失败”和“msg”值。因此,如果需要通知用户,那么我可以告诉用户它失败了,也许是为什么。

希望有帮助

// 长矛

【讨论】:

    【解决方案2】:

    我猜你的配置文件中没有启用查询字符串,可能这对你有用,

    javascript:

    $(function(){
        $.getJSON("<?php echo base_url();?>postC/latest_comment/" + post_id, function(res){
            $("#result").prepend(res.text);
        });
    });
    

    在 php 控制器中:

    function latest_comment($post_id)
    {
        $post_id        = $this->checkValues($post_id);
        $latestcomment  =  $this->models_facade->getLatestCommentForPost($post_id);
    
        // Better practise to put json header
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: application/json');
    
        return json_encode($latestcomment);
    }
    

    【讨论】:

      【解决方案3】:

      这就是我处理 jQuery Codeigniter 调用的方式,它对我有用。

      $(function(){
              $.post("<?php echo base_url();?>postC/latest_comment", {post_id: post_id}, 
                 function(data){
                    $("#result").prepend(data.html);
                 },'json');
          });
      

      PHP:

      function latest_comment(){
          $post_id        = $this->checkValues(**$_GET['post_id']**);
          $jsonData['html']  =  $this->models_facade->getLatestCommentForPost($post_id);
          echo json_encode($jsonData);
      }
      

      这将允许您在返回的 JSON 对象中添加更多内容。我喜欢使用的一个技巧是向数组添加“失败”和“msg”值。因此,如果需要通知用户,那么我可以告诉用户它失败了,也许是为什么。

      希望有帮助

      在这个答案中
      $_POST['post_id'] 适合我的情况,而不是 $_GET['post_id']

      【讨论】:

        【解决方案4】:

        我的朋友我也有类似的情况。而不是使用 return encode json 尝试回显结果。你基本上需要的是一个现成的 json 数据文件和解析它的 js 脚本。在您的情况下,结果只是浮动并等待在视图中回显。希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-05-09
          • 2017-11-20
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          • 2017-02-06
          相关资源
          最近更新 更多