2014-04-05 74 views
2

大家好,我在sencha應用程序的工作,我需要調用web服務點擊按鈕,我需要解析json。如何在sencha中調用web服務並解析json?

我有谷歌這很多,但沒有得到任何一步一步的教程。我對sencha很新,需要一步一步的教程來調用Web服務並在sencha touch中解析json。

我也嘗試了一些代碼,低於:

我的功能:

getDetails: function(){ 

       alert("Hi Welcome To Sencha"); 
        Ext.Ajax.request({ 
         method: 'GET', 
         url: 'http://www.aswaq.se/web_ajax_disp.php?fid=100&n=rt&p=rt', 
         params: { method: 'Helloworld', format: 'json' }, 
         success: function (response, request) { 
          alert('Working!') 
          alert(response.responseText) 
          console.log('Response:-' + response.responseText) 
         }, 
         failure: function (response, request) { 
          alert('Not working!') 
          console.log('Response Status:- ' + response.status) 
         } 
        }); 
     } 

幫助,將不勝感激。

回答

1

簡單直接的回答你的問題是:

Ext.Ajax.request({ 
    method: 'GET', 
    url: 'http://www.aswaq.se/web_ajax_disp.php?fid=100&n=rt&p=rt', 
    params: { method: 'Helloworld', format: 'json' }, 
    success: function (response, request) { 
     alert('Working!') 
     var jsonData = Ext.util.JSON.decode(response.responseText); 
    }, 
    failure: function (response, request) { 
     alert('Not working!') 
     console.log('Response Status:- ' + response.status) 
    } 
}); 

但煎茶觸摸2和ExtJS的指引reccomend使用stores: 它看起來是這樣的:

var myStore = Ext.create("Ext.data.Store", { 
    model: "User", 
    proxy: { 
     type: "ajax", 
     url : "/users.json", 
     reader: { 
      type: "json", 
      rootProperty: "users" 
     } 
    }, 
    autoLoad: true 
}); 

當你加載商店時,它會自動調用指定的正確url並根據您指定的模型(model:User)加載數據。 有兩種方法可以用來加載商店: set autoLoad:如示例中的true,並讓商店加載時加載到您的應用程序中 當您需要時調用myStore.load()