Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter 使用ajax从控制器检索数据_Codeigniter - Fatal编程技术网

Codeigniter 使用ajax从控制器检索数据

Codeigniter 使用ajax从控制器检索数据,codeigniter,Codeigniter,下面的代码在ajax响应中返回空白,请帮助我 当我检查控制器时,它也会给我空白。 您能检查下面的代码以找出问题的原因吗 以下是我的ajax代码: window.onload = function() { $.ajax({ type:'json', url:"http://localhost/myapne/admin/adminMenu/getMsg", success:function(data){ alert(

下面的代码在ajax响应中返回空白,请帮助我 当我检查控制器时,它也会给我空白。 您能检查下面的代码以找出问题的原因吗

以下是我的ajax代码:

window.onload = function() {
     $.ajax({
        type:'json',
        url:"http://localhost/myapne/admin/adminMenu/getMsg",

        success:function(data){
            alert(data);
           // PrintSms(data);
        },
          error: function(error){
      console.log(error);   
                    }
    });
}

这是我的控制器:

class AdminMenu extends CI_Controller{
function getMsg(){

    $this->load->model('adminGetModel');
    $data = $this->adminGetModel->getSms();
    return array("status"=>"success","rows"=>$data);
} 
}

这是我的模型:

class AdminGetModel extends CI_Model{

function getSms(){
//        $a = $count*10;
//        $b = $a + 10;
    $this->load->database();
       $query = $this->db->get('tblsms');
       $rows = array(); //will hold all results

    foreach($query->result_array() as $row)
   {    
    $rows[] = $row; //add the fetched result to the result array;
   }

     return $rows; 
   }
 }

Json_编码数据并使用echo而不是return:

echo json_encode(array("status"=>"success","rows"=>$data));
这将返回一个字符串。如果要将其转换回对象,则必须在ajax成功处理程序中使用
JSON.parse()
(如果使用jquery,则必须使用
$.parseJSON