没有根和跨域无法让 JSON 正确读取
Unable to Get JSON to Read Properly Without Root and Cross Domain
我问这个只是因为我似乎已经尝试了所有我能找到的东西,而且它必须比我做的更容易。
我正在使用 Sencha Touch(EXTJS 知识仍然有帮助)。当我console.log商店时,它显示没有items/data。我从商店的加载呼叫中得到了很好的响应,但它没有进入商店。感谢您的帮助。
型号:
Ext.define('NQT.model.NENEventsModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'regionId', type: 'int', mapping: 'regionId'},
{name: 'regionName', type: 'string', mapping: 'regionName'},
{name: 'state', type: 'string', mapping: 'state'},
{name: 'switchId', type: 'int', mapping: 'switchId'},
{name: 'switchName', type: 'string', mapping: 'switchName'}
]
});
商店:
Ext.define('NQT.store.NENEventsStore',
{
extend: 'Ext.data.Store',
storeId: 'NENEventsStore',
config: {
model: 'NQT.model.NENEventsModel',
proxy: {
type: 'jsonp',
url: 'http://this_is_not_the_url.com:8080/rest/json',
method: 'GET',
pageParam: false,
startParam: false,
limitParam: false,
noCache: false,
reader: {
type: 'json',
rootProperty: ''
}
}
}
});
部分视图:
var eventsStore = Ext.create('NQT.store.NENEventsStore');
{
xtype: 'grid',
titleBar: false,
store: eventsStore,
columns: [
{text: 'regionId', dataIndex: 'regionId', width: 150},
{text: 'regionName', dataIndex: 'regionName', width: 150},
{text: 'state', dataIndex: 'state', width: 150},
{text: 'switchId', dataIndex: 'switchId', width: 150},
{text: 'switchName', dataIndex: 'switchName', width: 150}
]
},
{
xtype: 'button',
docked: 'bottom',
text: 'Reload Grid',
handler: function () {
console.log(eventsStore);
eventsStore.load();
}
}
JSON 样本:
[{"regionId":1,"regionName":"NY Metro","state":"NJ","switchId":167,"switchName":"Jersey City 1"},
{"regionId":1,"regionName":"NY Metro","state":"NY","switchId":2029,"switchName":"Farmingdale 1"},
{"regionId":4,"regionName":"New England","state":"CT","switchId":203,"switchName":"Wallingford 1"}]
正如 Evan Trimboli 所指出的,我的反应并没有我想象的那么好;它缺少应该在它之前的 Ext.data.JsonP.callback 。我为 URL 创建了一个网络服务,这样我就可以正确地进行调用而不会有安全风险。
部分服务:
<?php
$app->group('/secret', function () use ($app) {
$app->get('/getData', function () use ($app) {
$callback = $app->request()->get('callback');
$events = file_get_contents("http://example_url.com:8080/this/is/the/path");
if(!empty($callback)){
$app->contentType('application/javascript');
echo sprintf("%s(%s)", $callback, $events);
} else {
echo $events;
}
});
});
?>
我问这个只是因为我似乎已经尝试了所有我能找到的东西,而且它必须比我做的更容易。
我正在使用 Sencha Touch(EXTJS 知识仍然有帮助)。当我console.log商店时,它显示没有items/data。我从商店的加载呼叫中得到了很好的响应,但它没有进入商店。感谢您的帮助。
型号:
Ext.define('NQT.model.NENEventsModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'regionId', type: 'int', mapping: 'regionId'},
{name: 'regionName', type: 'string', mapping: 'regionName'},
{name: 'state', type: 'string', mapping: 'state'},
{name: 'switchId', type: 'int', mapping: 'switchId'},
{name: 'switchName', type: 'string', mapping: 'switchName'}
]
});
商店:
Ext.define('NQT.store.NENEventsStore',
{
extend: 'Ext.data.Store',
storeId: 'NENEventsStore',
config: {
model: 'NQT.model.NENEventsModel',
proxy: {
type: 'jsonp',
url: 'http://this_is_not_the_url.com:8080/rest/json',
method: 'GET',
pageParam: false,
startParam: false,
limitParam: false,
noCache: false,
reader: {
type: 'json',
rootProperty: ''
}
}
}
});
部分视图:
var eventsStore = Ext.create('NQT.store.NENEventsStore');
{
xtype: 'grid',
titleBar: false,
store: eventsStore,
columns: [
{text: 'regionId', dataIndex: 'regionId', width: 150},
{text: 'regionName', dataIndex: 'regionName', width: 150},
{text: 'state', dataIndex: 'state', width: 150},
{text: 'switchId', dataIndex: 'switchId', width: 150},
{text: 'switchName', dataIndex: 'switchName', width: 150}
]
},
{
xtype: 'button',
docked: 'bottom',
text: 'Reload Grid',
handler: function () {
console.log(eventsStore);
eventsStore.load();
}
}
JSON 样本:
[{"regionId":1,"regionName":"NY Metro","state":"NJ","switchId":167,"switchName":"Jersey City 1"},
{"regionId":1,"regionName":"NY Metro","state":"NY","switchId":2029,"switchName":"Farmingdale 1"},
{"regionId":4,"regionName":"New England","state":"CT","switchId":203,"switchName":"Wallingford 1"}]
正如 Evan Trimboli 所指出的,我的反应并没有我想象的那么好;它缺少应该在它之前的 Ext.data.JsonP.callback 。我为 URL 创建了一个网络服务,这样我就可以正确地进行调用而不会有安全风险。
部分服务:
<?php
$app->group('/secret', function () use ($app) {
$app->get('/getData', function () use ($app) {
$callback = $app->request()->get('callback');
$events = file_get_contents("http://example_url.com:8080/this/is/the/path");
if(!empty($callback)){
$app->contentType('application/javascript');
echo sprintf("%s(%s)", $callback, $events);
} else {
echo $events;
}
});
});
?>