簡體   English   中英

如何在extjs中加載json

[英]how to load json in extjs

我正在加載來自Web服務的值,但出現以下錯誤消息。 ChatStore.data.items [i]未定義。

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"}

                ]        

     }); 

用於從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;
}

我的網絡服務結果是:-----

{
    "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"
            }
        ]
    }
}

我不知道哪里出了問題。

您不能只是將項目分配給store.items數組。 首先,它是不確定的。 其次,這不是整體正確的方法。

您需要在商店中指定Json代理對象,然后商店將自動加載自身,並且所有項目將被填充而無需任何特殊代碼,或者

您需要使用store.add()函數將新項目添加到商店。

從您的代碼看來,您正在使用store.load()方法,不是嗎? 您是否可以發布更多代碼,以了解如何准確加載數據以及與服務器的通信方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM