CI笔记6 json 传值

CI3.x 使用json,配合easyui,

其实很简单,走了很多的弯路,

首先在ci的控制器重,建立2个方法,一个用于显示加载view,一个用于echo json,就可以了。

需要注意的是,在ci的配置中config文件中,要配置base_url ,而且,在引用时localhost和127.0.0.1不能互用,

这一段,是ci的php控制器中的方法

public function demojson()
    {
        $this->load->database();
        $query = $this->db->query('select id,nav_name,nav_desc from nav');
        $row = $query->result_array();
        $json = json_encode($row);
        echo $json;
    }

public function demo2()
    {
        $this->load->view("demo2.html");
    }

 
这是view文件,其中的site_url文件,为test。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>demo2</title>
    <link rel="stylesheet" href="http://localhost/easyui/themes/default/easyui.css">
    <link rel="stylesheet" href="http://localhost/easyui/themes/icon.css">
    <script type="text/javascript" src="http://localhost/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="http://localhost/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="http://localhost/public/demo2.js"></script>
</head>
<body>
    <?php echo  site_url("demo/demojson"); ?>
    <table id="dg"></table>
</body>
</html>
 
这个是demo2.js文件,url在引用数据的时候,只需要传递方法名,传值,即可。
$(function(){
    $('#dg').datagrid({
            url:'demojson',
        columns:[[
            {field:'id',title:'id',100},
            {field:'nav_name',title:'name',200},
            {field:'nav_desc',title:'描述',300}
        ]]    
    });

});

最终运行结果:

原文地址:https://www.cnblogs.com/sdgtxuyong/p/5482671.html