2012-12-04 40 views
0

我对这个项目的目标是装载,用一个脚本输出的Web服务器上的Json,这样我可以在煎茶触摸在列表中显示的数据2.在煎茶触摸商店使用代理2

我我查看了无数的例子,包括来自其他人在这个网站上提出的问题的答案,我仍然无法弄清楚我的代码出现了什么问题。我相信这是一件非常小的事情,或者是我不了解的一些规则,但我希望有人能指出我正确的方向。

这里是我的模型:

Ext.define('Sencha.model.Location', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: ['name','location','open','details'] 
    } 
}); 

这里是我的商店:

Ext.define('Sencha.store.Locations',{ 
    extend: 'Ext.data.Store', 
    requires:[ 
     'Sencha.model.Location', 
    ], 
    config: { 
     model: 'Sencha.model.Location', 
     storeId: 'Locations', 
     proxy: { 
     type: 'ajax', 
     url : 'http://url/to/locations.php?filetype=.json', 
     reader: { 
     type: 'json', 
     }, 
     autoLoad: 'true' 
    } 
    } 
}); 

这里就是我希望它显示出来的观点:

Ext.define('Sencha.view.LocationList',{ 
    extend: 'Ext.List', 
    alias: 'widget.LocationList', 
    xtype: 'locationlist', 
    config: { 
     title: 'What\'s Open @CU', 
     disableSelection: true, 
     itemTpl: '<img src="http://localhost/{open}.png" style="width:16px;height:16px;margin-right:8px;" />{name}<span style="font-size:9pt;margin-left:8px;color:#888;">{location}</span>', 
     store: 'Locations', 
     onItemDisclosure: true 
    } 
}); 

这里是一个的输出的JSON(也许这是一个格式问题,导致它默默地失败?)

{ 
"businesses": 
    { 
    "name" : "Baker's" 
    "location" : "4th Floor Uni Centre" 
    "open" : "open" 
    "details" : "This is some information." 
    } 
} 
+0

在控制台中的任何错误? –

+0

“http://url/to/locations.php”在同一个域中吗?没有CORS问题? – ThinkFloyd

回答

1

您的JSON无效。你忘了逗号:

{ 
"businesses": 
    { 
    "name" : "Baker's", 
    "location" : "4th Foor Uni Centre", 
    "open" : "open", 
    "details" : "This is some information." 
    } 
} 

另外,如果你打算发送一个以上的业务,你可能希望将JSON格式更改为这样的事情:

{ 
"businesses":[ 
    { 
    "name" : "Baker's", 
    "location" : "4th Foor Uni Centre", 
    "open" : "open", 
    "details" : "This is some information." 
    }, 
    { 
    "name" : "Baker's", 
    "location" : "4th Foor Uni Centre", 
    "open" : "open", 
    "details" : "This is some information." 
    } 
    ... 
] 
} 

,然后添加rootProperty: 'businesses'到你是代理的读者,就像nuthan说的那样。

希望这有助于

+0

谢谢,逗号绝对是问题的一部分!我也最终改变了如何生成JSON并使用JsonP,就像上面的答案中的一个所示。看起来很愚蠢的是,如果没有告诉你它发现Json存在问题,它会默默地失败,其余的错误捕获非常好!感谢您的回答 :) –

1

rootProperty:'businesses'加入您的店铺的reader: { type: 'json', rootProperty:'businesses' }

+0

然后,企业必须是一个JSON对象数组 –

+0

我最初是这样做的,但它没有什么区别。问题解决了,下面的答复者指出了一大堆问题。无论如何,谢谢各位 –

0

http://url/to/locations.php 

相同的位置,你的煎茶触摸应用程序? 如果不是,则需要JsonP代理。 JsonP将Json数据封装到上下文中的函数中以便可用。

+0

我结束了使用JsonP。我已经达到了使用ajax工作的地步,但是对于发行版,我计划让json在不同的服务器上输出,所以我不得不学习如何使用JsonP。谢谢:) –