【问题标题】:PHP Script to return JSONP for Sencha Touch为 Sencha Touch 返回 JSONP 的 PHP 脚本
【发布时间】:2013-05-20 08:08:31
【问题描述】:

我使用 Sencha Touch 开发了一个应用程序,它将在我的服务器上使用 JSONP 获取数据。

这是我在煎茶的店铺

config: {
storeId: 'actor',
autoLoad: true,
model: 'App.model.actor',
proxy:{
    type: 'jsonp',
    url: 'http://domain.com/actorjson.php?getActors',

    reader: {
        type: 'json',
        rootProperty: 'items.feed'
    },

},

当请求使谷歌浏览器显示这样的时候

http://domain.com/actorjson.php?getActors&_dc=1369408455693&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback95

到目前为止,一切看起来都很好,并且应用程序运行良好。

我的问题是:我有超过 1000 个条目要从 PHP 返回,在这种情况下,加载所有条目都是无用的, 所以我想使用 PAGE 功能,我对 PHP 很陌生,你能指出我的 PHP 脚本可以与分页一起使用吗 任何例子都会很有帮助。

谢谢 帕万

【问题讨论】:

    标签: php extjs pagination sencha-touch-2 jsonp


    【解决方案1】:

    您在服务器端要做的就是读取请求的limit 参数以及startpagestart tell 是要包含的第一个项目的索引,而 page 是页面的索引...如您所见,默认情况下,Ext 将它们都发送。 start 更容易与 MySQL 等一些数据库一起使用,因为它们使用相同的参数。

    为了使用 GET 方法(即在 URL 中)访问请求参数发送,在 PHP 中,您可以使用名为 $_GET 的“神奇”全局变量(还有一个 $_POST 变量和一个 $_REQUEST 变量,其中 GET 和 POST 参数都可用)。

    话虽如此,在不了解您的数据源和数据模型的情况下,不可能为您提供一个可以与您的 Javascript 一起使用的服务器脚本示例...

    这里的一般原则:

    <?php
    
    // --- Source data ---
    
    $records = array(
        array('id' => 1, 'name' => 'eric', 'age' => 30),
        array('id' => 2, 'name' => 'foo', 'age' => 99),
        array('id' => 3, 'name' => 'bar', 'age' => 18),
        // trailling commas in array are OK in PHP ;)
    
        // let's pretend there's 1000 of them
    );
    $nRecords = count($records);
    
    // --- Read request params ---
    
    $defaultLimit = 25;
    // isset($variable) ensures that the variable exists in order to
    // avoid a fatal error
    $limit = isset($_GET['limit']) ? $_GET['limit'] : $defaultLimit;
    $start = isset($_GET['start']) ? $_GET['start'] : 0;
    
    // --- Construct returned item array ---
    
    $items = array();
    for ($i = 0; $i < $limit; $i++) {
        $index = $start + $i;
    
        // leave the loop if we've reached the end of the source array
        if ($index >= $nRecords) {
           break; 
        }
        // or push the record in the item array
        else {
            $items[] = $records[$index];
        }
    }
    
    // --- Construct and send the response ---
    
    $responseData = array(
        'total' => count($items),
        // using the root property you've configured
        'items.feed' => $items,
    );
    
    // Encode response data to JSON
    $jsonData = json_encode($responseData);
    
    // If a callback is specified, the request has been made by a
    // JsonP proxy
    if (isset($_REQUEST['callback'])) {
        $callback = $_REQUEST['callback'];
        // HTTP response header
        header('Content-Type: text/javascript');
        // Encode to JSON
        // string concatenation is done with dots in PHP
        echo $callback . '(' . $jsonData . ')';
    }
    // Else we'll return raw json
    else {
        // HTTP response header
        header('Content-Type: application/x-json');
        // Encode to JSON and write to output
        echo $jsonData;
    }
    
    // --- We're done ---
    

    一般startlimit 参数应用于数据库引擎级别,以节省服务器端的内存,避免无用的处理......我希望你能适应在这里。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 2012-05-26
      • 2012-09-05
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      相关资源
      最近更新 更多