0

我开始了解 Sencha Touch 2。所以,我有很多问题要问!^^让我们研究一下。现在我有一个数据json,例如:

{结果:“成功”,国家:[“阿富汗”,“阿尔巴尼亚”,“阿尔巴尼亚”,“阿尔及利亚”,“美属萨摩亚”,“安道尔”] }

然后,我将从 url:nation.php 文件加载它。我怎样才能将它加载到我的选择字段.??????

分享给我,支持我!谢谢 :)。

4

2 回答 2

2

我不知道如何在 Sencha Architect 2 中执行此操作(我没有使用它).. 但仍然

与其不尝试就问问题(我的意思是你没有在这里发布尝试过的代码),不如从 Sencha Touch Documentation 开始。

无论如何,你可以这样做

模型

Ext.define('AppName.model.countries', {
    extend : 'Ext.data.Model',

    config: {
        fields: [
               {name: 'name', convert: function(value, record) {
                                return record.raw;
               }}
            ],
    }
});

店铺

var myStore = Ext.create("Ext.data.ArrayStore", {
  model : 'AppName.model.countries',
  proxy: {
    type: "ajax",
    url : "nation.php",
    reader: {
        type: 'json',
        rootProperty : function(data) {
            return data.national;
        }
    }        
    },
    autoLoad: true
 });

在视图中选择字段

 Ext.create('Ext.form.Panel', {
    fullscreen: true,
    items: [{
       xtype: 'selectfield',
       store: myStore ,
       valueField:'name',
       displayField:'name'
    }]
});
于 2013-06-11T12:42:36.663 回答
1

在维斯瓦的支持下。:) 我发现了这个问题 - XMLHttpRequest 无法加载。Access-Control-Allow-Origin 错误(浏览器策略安全)不允许来源。Sencha Touch 文档说:“当您需要从运行应用程序的域以外的域加载数据时,JsonP 代理很有用。如果您的应用程序在http://domainA.com上运行,则无法使用 Ajax 加载它的数据来自http://domainB.com,因为浏览器禁止跨域 ajax 请求。

” 另外,我们需要做的就是——“在您的 Web 服务器中实现所有 api”并遵循 JsonP 的格式代码:(在 PHP 中)

$callback = $_REQUEST['callback'];// check callbackkey

// Create the output object.
$output = array('a' => 'Apple', 'b' => 'Banana');// output data.

//start output
if ($callback) {
    header('Content-Type: text/javascript');
    echo $callback . '(' . json_encode($output) . ');';
} else {
    header('Content-Type: application/x-json');
    echo json_encode($output);
}

如果。使用 Sencha Touch 2.1,您可以使用:

Ext.data.JsonP.request({
        url: 'http://otherdomain/svn_visaapi/trunk/api/visa_api.php/test_json',
        callbackKey: 'callback',
        success: function(result) {
            console.log(result);
            //Your success function here...
        }
    });

- 如果,使用 Sencha Architect,您可以使用 Store.proxy.JsonP 调用 api。- 阅读更多文档 Sencha Touch 2.1 以了解这一点。

于 2013-06-18T08:13:31.950 回答