JQuery getJSON 从控制器检索数据的问题(代码点火器)

发布于 2024-10-26 14:39:01 字数 2001 浏览 1 评论 0原文


我的 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 代码 Igniter 控制器:

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 函数都可以正常工作。

有谁知道错误可能出在哪里?我已经调试了一整天,但没有结果。
谢谢

I have a problem with my JQuery, Ajax, and PHP code.
I have a page containing all the post from a user.
Then in this page, I want to load the latest comment for that post by using Ajax.
However it doesn't show up anything.

Here is the PHP code where I called the JQuery function:

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

<? } ?>

And this is my PHP Code Igniter Controller that handle the request:

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

I have tested to call the function manually and display the latest comment without using ajax, and it worked. So this means that there is nothing wrong with the back end function in the modelthat retrieve the result from mysql.
I have also tried to do :

echo json_encode($latestcomment);  

when trying to call the function manually, and I could see that the data was in JSON format.
So this means, that there is something wrong with the ajax call, that it did not get the data from the controller. I used GET for .getJSON, and POST for .post() and .ajax().

I have tried all versions of the jQuery Ajax call: .getJSON(), .post(), and .ajax() , and none of it worked.
I am sure that the corresponding Jquery libraries have been loaded properly, since all my other ajax functions could work properly.

Does anyone has any clue where the errors could be? I have been debugging this for one full day, and got no result.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-11-02 14:39:01

这就是我处理 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 对象中放入更多内容。我喜欢使用的一个技巧是向数组添加“failed”和“msg”值。因此,如果需要通知用户,我可以告诉用户失败以及原因。

希望有帮助

// 长矛

This is how I handle my jQuery Codeigniter calls and it works for me.

Try this for 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);
}

This will allow you to put more in the JSON object that is returned. One trick I like to use is adding a "failed" and "msg" value to the array. So if the user needs to be notified then I can tell the user it failed and maybe why.

Hope that helps

// lance

相思碎 2024-11-02 14:39:01

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

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

My guess is you have not enabled query string in your config file, may be this will work for you,

javascript:

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

and in php controller:

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);
}
萌︼了一个春 2024-11-02 14:39:01

这就是我处理 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 对象中放入更多内容。我喜欢使用的一个技巧是向数组添加“failed”和“msg”值。因此,如果需要通知用户,我可以告诉用户失败以及原因。

希望

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

This is how I handle my jQuery Codeigniter calls and it works for me.

$(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);
}

This will allow you to put more in the JSON object that is returned. One trick I like to use is adding a "failed" and "msg" value to the array. So if the user needs to be notified then I can tell the user it failed and maybe why.

Hope that helps

In this answer
$_POST['post_id'] is working fine for my case instead of $_GET['post_id']

清风挽心 2024-11-02 14:39:01

我的一位朋友也遇到了类似的情况。尝试回显结果,而不是使用返回编码 json。你基本上需要的是一个准备好的 json 数据文件和解析它的 js 脚本。在您的情况下,结果只是浮动并等待在视图中回显。希望有帮助。

My fellow friend I had a simillar situation. Instead of using return encode json try to echo out the result. What u basicly need is a ready json data file andjs script that parses it. In your case the result is just floating and waiting to be echo-ed in the view. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文