doucitan2544 2014-06-04 23:28
浏览 62
已采纳

Codeigniter ajax调用错误

I'm trying to make an ajax call to get result from my database, but i'm facing an error.

My javascript:

  1. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. <script language="Javascript">
  3. setTimeout(makeAjaxCall, 1000);
  4. function makeAjaxCall(){
  5. $.ajax({
  6. type: "post",
  7. url: "call/update",
  8. cache: false,
  9. data: {action: 'getUpdate', term: '<?php echo $id;?>'},
  10. success: function(json){
  11. try{
  12. var obj = jQuery.parseJSON(json);
  13. alert( obj['STATUS'] + obj['results']);
  14. }catch(e) {
  15. alert('Exception while request..');
  16. }
  17. },
  18. error: function(){
  19. alert('Error while request..');
  20. }
  21. });
  22. }
  23. </script>

And my controller's method:

  1. public function update()
  2. {
  3. if (isset($_POST['action'])){
  4. if ($_POST['action'] == 'getUpdate'){
  5. pollNewData();
  6. }
  7. }
  8. function pollNewData(){
  9. $term = $_POST['term'];
  10. $query = $this->db->query("SELECT * FROM users where guid <> '' and user_id = '$term'");
  11. $res = $query->result();
  12. echo json_encode(array('STATUS'=>200, 'results'=>$res));
  13. }
  14. }

i have this error on chrome debugs tool:

500 (Internal Server Error)

展开全部

  • 写回答

2条回答 默认 最新

  • douh9817 2014-06-05 00:00
    关注

    You have several issues. Below is the working code:

    public function update()
    {
    
        if(!function_exists('pollNewData')){ // don't redeclare if already exists
            function pollNewData($db){ // pass $db
                $term = $_POST['term'];
                $query = $db->query("SELECT * FROM users where guid  <> '' and user_id = '$term'");
                $res = $query->result();
                echo json_encode(array('STATUS'=>200, 'results'=>$res));
            }
        }
    
        if (isset($_POST['action'])){
            if ($_POST['action'] == 'getUpdate'){
                pollNewData($this->db); // pass $this->db
            }
        }
    
    }
    

    Changes:

    • Moved the function definition to before it is called - it must exist before calling.
    • The $this context is not set in the function, so pass the $db object as an argument.
    • When defining functions inside a class method, you must have a function_exists() check because on the second call, it will try to redeclare the function and produce a fatal error.

    For future debugging you should turn errors on:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部