2012-07-18 81 views
1

我是新來的ext Js。解碼Ajax響應

我有一個ajax調用。我可以看到警報上的響應文本,但下一行(假定解碼responseText)不會在警報框中產生任何結果。

我的功能是這樣的:

function openToRecipients() 
{ 

    Ext.Ajax.request({ 
     url: "Redirector?id=ClinicalInitiateForm&wfid=CLINICALONGOINGWFINITIATE", 
     method: 'POST',     
      success: function(response, opts) 
      { 
       alert(response.responseText); 
       var dataCurrent = Ext.util.JSON.decode(response.responseText); 
       alert(dataCurrent); 
       var jsonStr = dataCurrent.cData; 
       recipientJsonResponse = dataCurrent.dataGrid; 
       var myObject = eval('(' + jsonStr + ')'); 
       gridStore = new Ext.data.JsonStore({ 
        id : 'gridStore', 
        autoLoad : true, 
        data : myObject, 
        root : 'data', 
        fields:['NAME', 
           'CLIENT', 
           'DESCRIPTION' 
         ], 
        listeners :{ 
         load : gridDisplay 
        } 
       }); 
       }, 
      failure: function(response, opts) { 
       alert("fail"); 
      } 

    }); 

} 

這是coverting串

"formFields" : [ { 
    "id" : "NAME", 
    "set" : "", 
    "label" : "Name", 
    "dataType" : "string", 
    "editType" : "static", 
    "clientConfig" : "", 
    "hide" : "False", 
    "required" : "", 
    "mask" : "", 
    "maxValue" : "", 
    "maxLength" : "", 
    "minValue" : "", 
    "value" : "", 
    "showIf" : "", 
    "options" : "", 
    "prePopulate" : "", 
    "shortForm" : "", 
    "comments" : "", 
    "optionsValue" : "", 
    "currentValue" : "", 
    "disabled" : "", 
    "qTip" : "", 
    "hover" : "" 
    }, { 
    "id" : "CLIENT", 
    "set" : "", 
    "label" : "Client", 
    "dataType" : "string", 
    "editType" : "static", 
    "clientConfig" : "", 
    "hide" : "False", 
    "required" : "", 
    "mask" : "", 
    "maxValue" : "", 
    "maxLength" : "", 
    "minValue" : "", 
    "value" : "", 
    "showIf" : "", 
    "options" : "", 
    "prePopulate" : "", 
    "shortForm" : "", 
    "comments" : "", 
    "optionsValue" : "", 
    "currentValue" : "", 
    "disabled" : "", 
    "qTip" : "", 
    "hover" : "" 
    }, { 
    "id" : "DESCRIPTION", 
    "set" : "", 
    "label" : "Description", 
    "dataType" : "string", 
    "editType" : "static", 
    "clientConfig" : "", 
    "hide" : "False", 
    "required" : "", 
    "mask" : "", 
    "maxValue" : "", 
    "maxLength" : "", 
    "minValue" : "", 
    "value" : "", 
    "showIf" : "", 
    "options" : "", 
    "prePopulate" : "", 
    "shortForm" : "", 
    "comments" : "", 
    "optionsValue" : "", 
    "currentValue" : "", 
    "disabled" : "", 
    "qTip" : "", 
    "hover" : "" 
    } ], 

後,我的JSON和這是我的數據

{'data':[{"NAME":"Shan","CLIENT":"CSC","DESCRIPTION":"Computer science"}]} 

我怎麼能有這樣的數據在我的網格中

+0

答案是有效的Json?請提供,以便我們可以幫助回答這個問題。 – Brian 2012-07-18 12:39:26

+0

你是否想知道我的json是否有效?是的,這是 – 2012-07-18 12:41:57

+0

重點是要看看,否則我們無法幫助。我在上面的代碼中看不到任何明顯的錯誤,所以不可能知道什麼是錯的。所以發佈JSON響應。 – Brian 2012-07-18 12:50:44

回答

1

這裏是你可以使用代碼:

var myStore = Ext.create("Ext.data.JsonStore", { 
    fields: [ "firstname", "lastname" ], // the fields of each item (table line) 
    proxy: { 
     type: "ajax", // the proxy uses ajax 
     actionMethods: { // this config is not necessary for you. I needed to use it to be able to work with the echo service of jsFiddle. if you want to use post (as I saw in your post, you can skip this) 
      create: "POST", 
      read: "POST", 
      update: "POST", 
      destroy: "POST" 
     }, 
     url: "/echo/json/", // here will come your URL that returns your JSON (in your case "Redirector?id..." 
     reader: { 
      type: "json", // this store reads data in json format 
      root: "items" // the itens to be read are inserted in a "items" array, in you case "formFields" 
     } 
    } 
}); 

// in jsFiddle, we need to send the JSON that we want to read. In your case, you will just call .load() or set the autoLoad config of the store to true. If you want send adition parameters, you can use the sintax below. 
myStore.load({ 
    params: { 
     // everything inside the encode method will be encoded in json (this format that you must send to the store) 
     json: Ext.encode({ 
      items: [{ 
       "firstname": "foo", 
       "lastname": "bar" 
      }, { 
       "firstname": "david", 
       "lastname": "buzatto" 
      }, { 
       "firstname": "douglas", 
       "lastname": "adams" 
      }] 
     }) 
    } 
}); 

// creatin the grid, setting its columns and the store 
Ext.create("Ext.grid.Panel", { 
    title: "My Grid", 
    columns: [{ 
     header: "First Name", 
     dataIndex: "firstname" // the dataIndex config is used to bind the column with the json data of each item 
    }, { 
     header: "Last Name", 
     dataIndex: "lastname" 
    }], 
    store: myStore,   // the store created above 
    renderTo: Ext.getBody() // render the grid to the body 
}); 

你可以在這裏訪問小提琴:http://jsfiddle.net/cYwhK/1/ 文檔:

另一個人認爲我忘記告訴你可以在你的商店中使用模型而不是字段數組。模型就像一個面嚮對象語言的類。看看:http://dev.sencha.com/deploy/ext-4.1.0-gpl/docs/index.html#!/api/Ext.data.Model

+0

:感謝您的幫助,並感謝您的幫助 – 2012-07-19 07:07:34

+0

@ Jaisri_88,不客氣。現在我需要睡覺。現在是巴西的上午4點。當我醒來時,我會看看帖子,看看你是否能夠解決你的問題(我想你會的)。看到你和好運;) – davidbuzatto 2012-07-19 07:10:08

+0

@ david:唐知道如何謝謝你,一直在掙扎。你的例子真的幫了我。由於我是新來的,所以我甚至知道如何去問我的疑惑。成爲我的Ext js導師;) - – 2012-07-19 09:32:29