gpt4 book ai didi

javascript - ExtJS 抓取 JSON 结果

转载 作者:IT王子 更新时间:2023-10-29 00:10:39 26 4
gpt4 key购买 nike

我正在从 PHP 女巫生成 JSON 响应,如下所示:

{ done:'1', options: [{ message:'Example message'},{message:'This is the 2nd example message'}]}

我想使用 ExtJS 获取这些结果。这是我目前所拥有的:

Ext.Ajax.request({
loadMask: true,
url: 'myfile.php',
params: {id: "1"}
});

接下来我必须写什么才能得到这样的 json 结果:

var mymessages = jsonData.options;

mymessages 应包含示例消息,这是第二条示例消息。

谢谢。

最佳答案

直接的方法:

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);
}
}
});

或者您可以使用 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"));
});
}

再举一个例子来解决最后一条评论:

var messages = [{title: "1"},{title: "2"},{title: "3"}];

var titles = msg;
Ext.each(messages, function(msg){
titles.push(msg.title);
});
alert(titles.join(", "));

尽管我更喜欢使用 Array.map(Ext 不提供):

var text = messages.map(function(msg){
return msg.title;
}).join(", ");
alert(text);

关于javascript - ExtJS 抓取 JSON 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/944871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com