【问题标题】:Get attribute data from xml using sencha touch 2使用 sencha touch 2 从 xml 获取属性数据
【发布时间】:2012-05-09 18:47:44
【问题描述】:

我正在试验如何在使用 Phonegap 和 Sencha touch2 编程时从服务器获取数据。

我正在尝试从 Google Weather API 获取一些天气数据。

我有一个面板,其中包含一个列表,现在应该显示 day_of_week 数据。但我找不到如何从属性中获取数据。如果您查看http://www.google.com/ig/api?weather=zonhoven 的 xml,您可以看到星期几表示为:

所以我得到了以下代码:

Ext.define('WeerBe.view.mainTabs.TestPage', {
extend : 'Ext.navigation.View',
xtype : 'testPage2',

config : {
    title : 'Test Page2',
    iconCls : 'info',

    scrollable : true,

    items : [{
        xtype : 'list',
        itemTpl: '{day_of_week}',

        store : {
            autoLoad: true,

            fields : ['day_of_week'],


            proxy : {
                type : 'ajax',
                url : 'http://www.google.com/ig/api?weather=zonhoven',
                reader : {
                    type : 'xml',
                    root : 'weather',
                    record: 'forecast_conditions'
                }
            }
        }
    }]
}
})

列表应该包含 4 条记录,但它不显示数据,因为 day_of_week 节点本身是空的,并且数据在 data 属性中。

如何获取属性数据?

【问题讨论】:

    标签: xml cordova sencha-touch-2


    【解决方案1】:

    我运行了您的代码并检查了控制台日志。

    这是抛出的错误:

    XMLHttpRequest 无法加载 http://www.google.com/ig/api?weather=zonhoven&_dc=1336560194441&page=1&start=0&limit=25。 来源http://localhost.com 不允许 访问控制允许来源。

    该错误是因为您正在发出 ajax 请求,其中包含 Cross-domain cross-origin resource sharing.

    所以,将代理类型更改为'jsonp'

    proxy : {
          type : 'jsonp',
          url : 'http://www.google.com/ig/api?weather=zonhoven',
          ....
          ....
    

    【讨论】:

    • 奇怪,我之前没有收到此错误,现在我也收到了。但是,当我将 ajax 更改为 jsonp 时,我在控制台中收到以下错误: Uncaught SyntaxError: Unexpected token
    • 没关系。 jsonp request 仅用于跨域请求数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2023-04-08
    • 2012-06-03
    • 1970-01-01
    相关资源
    最近更新 更多