简体   繁体   English

如何在extjs中加载json

[英]how to load json in extjs

i am loading the values coming from web service but i get following error message. 我正在加载来自Web服务的值,但出现以下错误消息。 ChatStore.data.items[i] is undefined. ChatStore.data.items [i]未定义。

Mapping code for extjs is extjs的映射代码是

ChatStore = new Ext.data.JsonStore({
         storeId: 'ChatStore1',
         fields: [
                    {name: "pic_url", mapping: "sender_pic_url"}, 
                   {name: "first_name", mapping: "first_name"},
                    {name: "last_name", mapping: "last_name"},
                    {name: "message_text", mapping: "message_text"},
                    {name: "time", mapping: "time"}

                ]        

     }); 

code for loading data from webservice 用于从Web服务加载数据的代码

Ext.Ajax.request({
        url: 'webservice call',
         params: Ext.encode('{"sender_user_id":"' + suid + '","receiver_user_id":"' + ruid + '"}'),
         headers: { 'Content-type': 'application/json;charset=utf-8' },
        success: function (result, request) {

            retData = (result.responseText);
            alert(retData);
             retData = eval("(" + retData + ")");
             if (retData.status == 'success') {


                 //ChatStore.loadData(retData.result.messages);
                 //ChatStore.loadData(retData.result);
                 for (var i = 0; i < retData.result.messages.length; i++) {
                     if (retData.result.messages[i].message_from == "YES") {

                         ChatStore.data.items[i].data.pic_url = retData.result.sender_pic_url;
                         ChatStore.data.items[i].data.first_name = retData.result.sender_name;


                    }
                     else {
                        ChatStore.data.items[i].data.pic_url = retData.result.receiver_pic_url;
                         ChatStore.data.items[i].data.first_name = retData.result.reciever_name;
                    } 

                     ChatStore.data.items[i].data.message_text = retData.result.messages[i].message_text;
                    ChatStore.data.items[i].data.time = retData.result.messages[i].time;

                }
                // ChatPanel.render('divchat');

                 messagePanel.hide();
                ChatPanel.show();
            }


             else { alert('error loading first radius'); }

         },
        failure: function (result, request) {

             alert("Error: " + result.statusText);
         }

         });  
return false;
}

My webservice result is:----- 我的网络服务结果是:-----

{
    "status": "success",
    "message": "Messages are listing below",
    "result": {
        "sender_name": "Paul",
        "reciever_name": "Clay",
        "sender_pic_url": "Images/f1.jpg",
        "receiver_pic_url": "Images/f2.jpg",
        "messages": [
            {
                "message_from": "YES",
                "message_text": "hi, how r u?",
                "time": "12:00am"
            },
            {
                "message_from": "NO",
                "message_text": "hi, where are you?",
                "time": "1:00am"
            },
            {
                "message_from": "NO",
                "message_text": "Dear, you are invited for the meeting",
                "time": "2:00am"
            },
            {
                "message_from": "YES",
                "message_text": "hi, how r u?",
                "time": "12:00am"
            },
            {
                "message_from": "NO",
                "message_text": "hi, where are you?",
                "time": "1:00am"
            },
            {
                "message_from": "NO",
                "message_text": "Dear, you are invited for the meeting",
                "time": "2:00am"
            },
            {
                "message_from": "NO",
                "message_text": "Ya ill be there",
                "time": "2:00am"
            },
            {
                "message_from": "NO",
                "message_text": "at what time party starts",
                "time": "2:00am"
            },
            {
                "message_from": "YES",
                "message_text": "6Oclock in the evening",
                "time": "2:00am"
            },
            {
                "message_from": "NO",
                "message_text": "Who will the chief guest",
                "time": "2:00am"
            },
            {
                "message_from": "YES",
                "message_text": "our city mayor",
                "time": "2:00am"
            }
        ]
    }
}

i dont know where the things go wrong. 我不知道哪里出了问题。

You can't just assign items to the store.items array. 您不能只是将项目分配给store.items数组。 First of all it's undefined. 首先,它是不确定的。 Second, it's not correct approach overall. 其次,这不是整体正确的方法。

You need either to specify Json proxy object in the store and then store will load itself automatically and all items will be populated without any special code, or 您需要在商店中指定Json代理对象,然后商店将自动加载自身,并且所有项目将被填充而无需任何特殊代码,或者

You need to use store.add() function to add new items to the store. 您需要使用store.add()函数将新项目添加到商店。

From your code looks like you're using store.load() method, no? 从您的代码看来,您正在使用store.load()方法,不是吗? Can you post more code on how exactly you load data and how you're communication with the server looks like. 您是否可以发布更多代码,以了解如何准确加载数据以及与服务器的通信方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM