donglinxin8765 2015-07-10 13:11
浏览 66
已采纳

ExtJS 5.1,使用PHP从数据库加载网格数据

I am doing an online course of Ext JS 5.1 and in this exercise I have a service.php file which connects to database and returns JSON Data. I try to show this data but it never gets displayed (doesn't show any error message in firebug) and I can`t find what is the problem with the code.

This is the code of the store:

var storeUsuario = Ext.create('Ext.data.Store', {
    model : 'js.clase.Usuario',
    proxy: {
            type: 'rest',
            url: 'service.php?method=getUsuarios',
        reader: {
            type: 'json',
        }
    },
    autoLoad: true
});

This is the code of the grid:

    Ext.create('Ext.grid.Panel', {
        renderTo: Ext.getBody(),
        store: storeUsuario,
        width: 600,
        height: 400,
        title: 'Grid Usuarios',
        columns: [
            {
                text: 'id',
                dataIndex: 'id'
            },{
                text: 'nombre',
                width: 100,
                dataIndex: 'nombre'          
            },{
                text: 'apellidos',
                dataIndex: 'apellidos'
            },{
                text: 'email',
                dataIndex: 'email'
            },{
                text: 'nacionalidad',
                dataIndex: 'nacionalidad'
            },{
                text: 'rutaAvatar',
                dataIndex: 'rutaAvatar'
            }
 ]
});

and the service.php method:

<?php
    header('Content-Type: text/html; charset=utf-8');

    $conexion = mysql_connect("127.0.0.1", "root", "");
    mysql_select_db("usertest", $conexion);

        $method = $_GET['method'];

    switch($method) {

    case "getUsuarios":
            $query = "SELECT * FROM Usuario ORDER BY id ASC";
            $result = mysql_query($query, $conexion) or die(mysql_error());

            $numRows = mysql_num_rows($result);

            $usuarios = array();

            if ($numRows > 0) {
                $i = 0;
                while ($row = mysql_fetch_assoc($result)) {

                    $usuarios[$i] = $row;

                    $i++;
                }
            }

            $usuariosJSON = json_encode($usuarios);

            echo $usuariosJSON; 
            break;
    }
?>
  • 写回答

2条回答 默认 最新

  • dtevhgk028372 2015-07-10 14:03
    关注

    Your PHP code doesn't return a valid JSon.

    See this fiddle works correctly: https://fiddle.sencha.com/#fiddle/q9k

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料