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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。