【问题标题】:ExtJS 5.1, grid load data from database with PHPExtJS 5.1,使用 PHP 从数据库中加载数据
【发布时间】:2015-07-21 02:55:40
【问题描述】:

我正在学习 Ext JS 5.1 的在线课程,在本练习中,我有一个 service.php 文件,它连接到数据库并返回 JSON 数据。我尝试显示此数据,但它从未显示(在 firebug 中不显示任何错误消息),我找不到代码有什么问题。

这是商店的代码:

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

这是网格的代码:

    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'
            }
 ]
});

service.php 方法:

<?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;
    }
?>

【问题讨论】:

  • php代码真的返回了什么吗?

标签: php extjs extjs5


【解决方案1】:

您的 PHP 代码未返回有效的 JSon。

看到这个小提琴正常工作:https://fiddle.sencha.com/#fiddle/q9k

【讨论】:

    【解决方案2】:

    根据this post,您应该指定标题。
    我建议你像这样返回一个“手工形成”的 json:

    $data = /** whatever you're serializing **/;
    $data_length = /* the length of the result according to the request response */;
    header('Cache-Control: no-cache, must-revalidate'); // just to be sure
    header('Content-Type: application/json');
    echo "{ \"success\": true, \"total\": $data_length, \"records\": " . json_encode($data) . " }";
    

    json 数据可能需要[]...

    echo "{ \"success\": true, \"total\": $data_length, \"records\": [ " . json_encode($data) . " ] }";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多