doucitan2544 2014-06-05 07: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:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="Javascript">
setTimeout(makeAjaxCall, 1000);
function makeAjaxCall(){
$.ajax({
    type: "post",
    url: "call/update",
    cache: false,               
    data: {action: 'getUpdate', term: '<?php echo $id;?>'},
    success: function(json){                        
    try{        
        var obj = jQuery.parseJSON(json);
        alert( obj['STATUS'] + obj['results']);


    }catch(e) {     
        alert('Exception while request..');
    }       
    },
    error: function(){                      
        alert('Error while request..');
    }
 });
}
</script>

And my controller's method:

public function update()
{
    if (isset($_POST['action'])){
        if ($_POST['action'] == 'getUpdate'){
            pollNewData();
        }
    }

    function pollNewData(){
        $term = $_POST['term'];
        $query = $this->db->query("SELECT * FROM users where guid  <> '' and user_id = '$term'");
        $res = $query->result();
        echo json_encode(array('STATUS'=>200, 'results'=>$res));
    }

}

i have this error on chrome debugs tool:

500 (Internal Server Error)

  • 写回答

2条回答 默认 最新

  • douh9817 2014-06-05 08: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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3