2014-01-15 119 views
2

如果與其他帖子非常相似,我會提前道歉,但是在完成一個多星期的工作後,我無法獲得此功能。最後得到webservice返回數據,我可以看到返回的數據,但商店不會加載它。除了一種情況:當我創建一個ArrayStore並將商店設置爲loadRawData時,我在網格中看到整個響應錯誤地全部顯示在一列中;但很高興看到數據。是否有人可以看看這個,看看爲什麼我的店不加載數據和填充網格:extjs ajax呼叫商店未加載

Ext.define('User', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     { name: 'connTime', type: 'string' }, 
     { name: 'userName', type: 'string' }, 
     { name: 'clientName', type: 'string' }, 
     { name: 'feedUrl', type: 'string' }, 
     { name: 'dconnTime', type: 'string' }, 
     { name: 'errMessage', type: 'string' }, 
     { name: 'ip', type: 'string' } 
    ] 
}); 

var myStore = Ext.create('Ext.data.Store', { 
    model: 'User' 
}); 


Ext.Ajax.request({ 
    url: 'http://dfsdfsfsfs/WCFService/WebService1.asmx/getValue', 
    method: 'GET', 
    success: function (response, options) { 
     var s = response.responseText; 
     Ext.MessageBox.alert(s, 'WOO WOO'); 
     myStore.loadData(s); 
    }, 
    failure: function (response, options) { 
     Ext.MessageBox.alert('FAILED MF', 'Unable to GET'); 
    } 
}); 



    var grid = Ext.create('Ext.grid.Panel', { 
    store: myStore, 
    stateful: true, 
    stateId: 'stateGrid', 
    columns: [ 
     { 
      text: 'Query', 
      width: 25, 
      sortable: false, 
      dataIndex: 'userName' 
     }, 
     { 
      text: 'Count', 
      width: 5, 
      sortable: true, 
      renderer: change, 
      dataIndex: 'ip' 
     }, 
     { 
      text: 'Last Updated', 
      width: 76, 
      sortable: true, 
      dataIndex: 'connTime' 
     }, 
     { 
      text: 'Disconnect Time', 
      width: 10, 
      sortable: true, 
      renderer: change, 
      dataIndex: 'dconnTime' 
     }, 
     { 
      text: 'Client', 
      width: 10, 
      sortable: true, 
      renderer: change, 
      dataIndex: 'clientName' 
     } 

    ], 
    height: 350, 
    width: 800, 
    title: 'Active Queries', 
    renderTo: 'grid-example', 
    viewConfig: { 
     stripeRows: true 
    } 
}); 

.....

我甚至試着這樣做的,並用不同的方式這一次我甚至不看Ajax響應返回:

var newStore = Ext.create('Ext.data.ArrayStore', { 
    model: 'User', 
    proxy: { 
     type: 'ajax', 
     url: 'http://dfsdfsfsfs/WCFService/WebService1.asmx/getValue', 
     reader: { 
      type: 'json', 
      root: 'd', 
      successProperty: 'success' 
     }, 
     success: function (response, options) { 
      var s = response.responseText; 
      Ext.MessageBox.alert(s, 'LA LA LA'); 
      newStore.loadData(s); 
     }, 
     failure: function (response, options) { 
      Ext.MessageBox.alert('FAILED AGAIN', 'SUCKS'); 
     } 
    } 
}); 

編輯:

服務器響應:

{"d":{"connTime":null,"userName":"101591196589145","clientName":null, 
    "feedUrl":null,"dconnTime":null,"errMessage":null,"ip":null}} 

「d」根目錄是由我手動添加的,並不存在於原始web響應中。我抓住它,並添加根:

{"connTime":null,"userName":"101591196589145","clientName":null,"feedUrl":null, 
"dconnTime":null,"errMessage":null,"ip":null}} 

編輯2:

Ext.define('User', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     { name: 'value', type: 'string' }, 
     { name: 'tag', type: 'string' } 
    ] 
}); 


var newStore = new Ext.data.JsonStore({ 
    model: 'User', 
    proxy: { 
     type: 'ajax', 
     url: 'http://localhost:52856/WCFService/WebService1.asmx/getRules', 
     reader: { 
      type: 'json', 
      root: 'rules', 
      idProperty: 'value'     
     }, 
     success: function (response, options) { 
      var s = response.responseText; 
      Ext.MessageBox.alert(s, 'LA LA LA'); 
      newStore.loadData(s); 
     }, 
     failure: function (response, options) { 
      Ext.MessageBox.alert('FAILED AGAIN', 'SUCKS'); 
     } 
    } 
}); 

    var grid = Ext.create('Ext.grid.Panel', { 
    store: newStore, 
    stateful: true, 
    columns: [ 

這是新的JSON我想:

{"rules":[{"value":"101591196589145","tag":"16"}, 
{"value":"102890809752267","tag":"16"},  
{"value":"107821192690513","tag":"16"}, {"value":"111517428886211","tag":"16"}, 
{"value":"117389718398171","tag":"16"},{"value":"12099149051","tag":"16"}, 

OK,我發現這可能是有問題ext.ajax.request調用。在發出請求時,商店甚至沒有定義,因此數據沒有加載。我怎麼能通過這個?

+0

正確的方法是在存儲上聲明代理並加載存儲。你的服務器響應是什麼樣的? –

回答

2

在這裏,我會給你一個例子,只是比較你的代碼。

// defining a model 
Ext.define('SampleModel', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'YOUR_ID', type: 'int'}, 
     {name: 'YOUR_NAME', type: 'string'} 
    ] 
}); 

// defining a store 
var storeDefinition = new Ext.data.JsonStore({ 
    model: 'SampleModel', 
    proxy: { 
     type: 'ajax', 
     url: '/your/url/path/data.php', 
     reader: { 
      // you MUST define root and idProperty 
      type: 'json', 
      root: 'rootList', 
      idProperty: 'YOUR_ID' 
     } 
    } 
}); 

這裏是重要的部分是rootidProperty。如您所述,rootjson對象的根節點。 idProperty是json對象中唯一的id。

要抓到successfailure響應,您應該返回successfailure數據在json對象中。沒有必要在reader部分指定success屬性。例如;

public function dnr_info() 
{ 
    $dnr = $this->input->get('dnr', TRUE); 
    $subsys = $this->input->get('subsys', TRUE); 
    $data['success'] = TRUE; 
    $data['data'] = $this->dnr->get_dnr_info($dnr, $subsys); 
    echo json_encode($data); 
} 

以上函數將返回JSON對象的success財產。

而且,這裏是從服務器返回的json對象。

{"success":true,"data":{"SUBSYS_ART_NR":"136451","ART_NR":"459993","ACTION_DESC":"BAKKAL AKT\u0130V\u0130TES\u0130 (176)","ACTIONS_NR":"130012676","START_DATE":"19.12.2013","STOP_DATE":"16.01.2014","DISCOUNT_TYPE":"SPECIAL PRICE","LEVEL_TYPE":"QUANTITY","LEVEL_IMPACT":"MULTIPLE","BASIS":"ARTICLE","LEVEL_1":"1 ADET","VALUE_1":"5,92","NWW_VK_PREIS":"6.69","KOLLI_VK_PREIS":"6.69"}} 
// here is the another sample for root which is articleList, idProperty which is ARTICLE_ID 
{"articleList":[{"ARTICLE_ID":"193","ART_NR":"225","ART_DESC":"27*1\/5LT.M.NEK.TAMEK.","SORTEN_TEXT":"ELMA","VAR":"1","GEBI":"1","SUBSYS_ART_NR":"225","NWW_VK_PREIS":"14.49","KOLLI_VK_PREIS":"14.49","DNR_ID":null,"STATUS":"0"}, 
+0

非常感謝詳細的分解。我將我的代碼改爲一個簡單的例子,將你的模板作爲模板使用,但仍然沒有。使用小提琴手我發現webservice甚至沒有被調用。 – vbNewbie

+0

你認爲這與異步調用有關:web服務返回的數據,但沒有及時創建存儲 – vbNewbie

+0

我已經使用了小提琴手,從我可以看到的是,甚至沒有調用的URL;但它是有效的,如果我粘貼在瀏覽器 – vbNewbie

1

不是真的知道你如何在這樣的狀態下得到了自己。在SDK下載中有很多加載網格的例子。代理沒有失敗/成功的方法。

小提琴here。工作代碼:

Ext.onReady(function() { 

    Ext.define('User', { 
     extend: 'Ext.data.Model', 
     fields: [{ 
      name: 'value', 
      type: 'string' 
     }, { 
      name: 'tag', 
      type: 'string' 
     }] 
    }); 


    var store = new Ext.data.Store({ 
     model: 'User', 
     proxy: { 
      type: 'ajax', 
      url: 'data.json', 
      reader: { 
       type: 'json', 
       root: 'rules' 
      } 
     } 
    }); 
    store.load(); 

    var grid = new Ext.grid.Panel({ 
     renderTo: Ext.getBody(), 
     width: 400, 
     height: 400, 
     store: store, 
     columns: [{ 
      text: 'Value', 
      dataIndex: 'value', 
      flex: 1 
     }, { 
      text: 'Tag', 
      dataIndex: 'tag' 
     }] 
    }); 
}); 
+0

感謝您的回覆。我終於想出了在使用鏈接之後關注實際的webservice,發現它必須是web服務,因爲它仍然返回包含的內容,儘管它看起來像json 。我將contentType更改爲json,但仍然沒有想到。但至少它消除了extjs代碼的問題。感謝您的其他建議。 – vbNewbie

+0

當時無法幫助您。祝你好運。 –