Man*_*era 17 javascript php json extjs
我正在从PHP女巫生成JSON响应,如下所示:
{ done:'1', options: [{ message:'Example message'},{message:'This is the 2nd example message'}]}
Run Code Online (Sandbox Code Playgroud)
我想使用ExtJS获取这些结果.这是我到目前为止:
Ext.Ajax.request({
loadMask: true,
url: 'myfile.php',
params: {id: "1"}
});
Run Code Online (Sandbox Code Playgroud)
接下来我要写什么才能得到像这样的json结果:
var mymessages = jsonData.options;
Run Code Online (Sandbox Code Playgroud)
mymessages应该包含Example消息,这是第二个示例消息.
谢谢.
Ren*_*soo 35
直截了当的方法:
Ext.Ajax.request({
loadMask: true,
url: 'myfile.php',
params: {id: "1"},
success: function(resp) {
// resp is the XmlHttpRequest object
var options = Ext.decode(resp.responseText).options;
Ext.each(options, function(op) {
alert(op.message);
}
}
});
Run Code Online (Sandbox Code Playgroud)
或者您可以使用Store以更多Ext-ish方式执行此操作:
var messages = new Ext.data.JsonStore({
url: 'myfile.php',
root: 'options',
fields: [
{name: 'text', mapping: 'message'}
],
listeners: {
load: messagesLoaded
}
});
messages.load({params: {id: "1"}});
// and when loaded, you can take advantage of
// all the possibilities provided by Store
function messagesLoaded(messages) {
messages.each(function(msg){
alert(msg.get("text"));
});
}
Run Code Online (Sandbox Code Playgroud)
再一个例子来解决最后的评论:
var messages = [{title: "1"},{title: "2"},{title: "3"}];
var titles = msg;
Ext.each(messages, function(msg){
titles.push(msg.title);
});
alert(titles.join(", "));
Run Code Online (Sandbox Code Playgroud)
虽然我更喜欢使用Array.map(Ext不提供):
var text = messages.map(function(msg){
return msg.title;
}).join(", ");
alert(text);
Run Code Online (Sandbox Code Playgroud)
使用成功和失败属性:
Ext.Ajax.request({
loadMask: true,
url: 'myfile.php',
params: {id: "1"},
success: function(response, callOptions) {
// Use the response
},
failure: function(response, callOptions) {
// Use the response
}
});
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅Ext API文档