问题JQuery getJSON从控制器检索数据(代码点火器) [英] Problem with JQuery getJSON retrieve data from Controller (Code igniter)

查看:155
本文介绍了问题JQuery getJSON从控制器检索数据(代码点火器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的JQuery,Ajax和PHP代码有问题。
我有一个页面包含用户的所有帖子。
然后在此页面中,我想使用Ajax加载该帖子的最新评论。



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

 < ;? foreach($ post_query as $ post_row){?> 
< li class =post_list>
<?php echo $ post_row ['text'];?>
< br />
最新评论:
< br />
< script type =text / javascript>
var post_id = $('#post_id_<?php echo $ post_row ['id'];?>')。
document.write(post_id); //只是为了检查post_id值是否未定义

$(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');
$ it-> 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格式。 br>
这意味着,ajax调用有问题,它没有从控制器获取数据。我使用GET for .getJSON,POST for .post()和.ajax()。



我试过所有版本的jQuery Ajax调用:.getJSON(),.post()和.ajax(),没有一个工作。
我相信相应的Jquery库已正确加载,因为所有其他的ajax函数可以正常工作。



任何人都有任何线索,错误可能在哪里?我已经调试了一整天,没有结果。

感谢

解决方案

如何处理我的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值添加到数组。



希望有助于



// lance



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

解决方案

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

这篇关于问题JQuery getJSON从控制器检索数据(代码点火器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆