如何将静态 JSON 从 PHP 返回到 extjs 存储

Posted

技术标签:

【中文标题】如何将静态 JSON 从 PHP 返回到 extjs 存储【英文标题】:How to return static JSON from PHP to extjs store 【发布时间】:2013-06-24 10:22:43 【问题描述】:

我有一个json 字符串,我想通过 php url 在 extjs 的网格存储中获取它。如何从 php 文件中将此字符串作为 json 返回。 像这样:

var store = Ext.create('Ext.data.Store', 
    id: 'store',
    model: 'ForumThread',
    remoteGroup: true,
    buffered: true,
    leadingBufferZone: 300,
    pageSize: 100,
    proxy: 
        type: 'jsonp',
        url: 'myJsonPhp.php', //this file should return json.
        reader: 
            root: 'topics',
            totalProperty: 'totalCount'
        ,
    autoLoad: true
);

我知道有一些方法可以从 php 返回 json,但是它们创建然后返回 json。我已经有 json..

*JSON 字符串:*

"totalCount":"6679","topics":["title":"XTemplate with in EditorGridPanel","threadid":"133690","username":"kpr@emco","userid":"272497","dateline":"1305604761","postid":"602876","forumtitle":"Ext 3.x: Help","forumid":"40","replycount":"2","lastpost":"1305857807","excerpt":"","title":"IFrame error "_flyweights is undefined"","threadid":"133571","username":"Daz","userid":"52119","dateline":"1305533577","postid":"602456","forumtitle":"Ext 3.x: Help","forumid":"40","replycount":"1","lastpost":"1305857313","excerpt":"","title":"Status bar error with IFrames","threadid":"134120","username":"Daz","userid":"52119","dateline":"1305857168","postid":"604220","forumtitle":"Ext 3.x: Bugs","forumid":"41","replycount":"0","lastpost":"1305857168","excerpt":"","title":"hellllllllllllllp,why it doesn't fire cellclick event after I change the cell value??","threadid":"133827","username":"aimer311","userid":"162000","dateline":"1305700219","postid":"603309","forumtitle":"Ext 3.x: Help","forumid":"40","replycount":"3","lastpost":"1305856996","excerpt":"","title":"Extjs 4.0 support for IE9","threadid":"122352","username":"extdev22","userid":"48017","dateline":"1296097557","postid":"565503","forumtitle":"Ext: Open Discussion","forumid":"6","replycount":"30","lastpost":"1305841535","excerpt":"","title":"Using XTemplate to send XML","threadid":"134102","username":"cayenne_08","userid":"237393","dateline":"1305841170","postid":"604153","forumtitle":"Ext 3.x: Help","forumid":"40","replycount":"0","lastpost":"1305841170","excerpt":"","title":"Open Source Development - The people you interact with....","threadid":"134093","username":"watrboy00","userid":"9862","dateline":"1305837814","postid":"604114","forumtitle":"Community Discussion","forumid":"68","replycount":"0","lastpost":"1305837814","excerpt":"","title":"What widgets are missing most in the ExtJs library?","threadid":"134091","username":"Andrew.Golik","userid":"32056","dateline":"1305837033","postid":"604102","forumtitle":"Community Discussion","forumid":"68","replycount":"0","lastpost":"1305837033","excerpt":"","title":"What widgets are missing most in the ExtJs library?","threadid":"134090","username":"Andrew.Golik","userid":"32056","dateline":"1305836975","postid":"604100","forumtitle":"Community Discussion","forumid":"68","replycount":"0","lastpost":"1305836975","excerpt":"","title":"What widgets are missing most in the ExtJs library?","threadid":"134089","username":"Andrew.Golik","userid":"32056","dateline":"1305836900","postid":"604099","forumtitle":"Community Discussion","forumid":"68","replycount":"0","lastpost":"1305836900","excerpt":""]

【问题讨论】:

【参考方案1】:

最简单的解决方案是直接将字符串返回到响应中(当然还要设置内容类型):

<?php

header("Content-Type: application/json");

print $my_json_string;

?>

【讨论】:

以上是关于如何将静态 JSON 从 PHP 返回到 extjs 存储的主要内容,如果未能解决你的问题,请参考以下文章

Extjs文件上传使用json服务?

ExtJS:返回 json 存储中的总行/记录

将 JSON 数据加载到 ExtJs 网格中

将 Json 从 Php 返回到 Swift Alamofire

Extjs4.2.1 - 将 json 加载到树形面板失败

EXTJS:如何在表单中插入图像