2012-05-06 31 views
0

在我的VIew中,我添加了一個按鈕,當我點擊它時,Controller類被調用。 View中的值(文本字段值)將通過Web服務的URL發送。然後服務器將返回如下所示的JSON字符串;從'Controller'類將值傳遞迴'View'

{「值」:「SUCCESS」}

現在,我需要發送此JSON字符串返回到視圖,視圖會提醒取決於JSON響應用戶。如果JSON返回SUCCESS,則將顯示Success警報,如果JSON返回FAIL,則將顯示Fail警報。

1.)在我的代碼中,我只能顯示VIEW的JSON響應,但是如何將它發送給控制器? (var text = response.responseText;顯示JSON響應)

2.)我怎麼能從視圖中分離字符串,並只從JSON響應中獲取SUCCESS或FAIL字符串?

按鈕的實施,從視圖類

xtype:'button', 
        id:'when_button_click', 
        text:'Send', 
        ui:'confirm', 

控制器類

Ext.define( 'myapp.controller.testcont',{

    extend: "Ext.app.Controller", 
        config: { 
        refs: { 
        newNoteBtn: "#when_button_click" 
        }, 
        control: { 
        newNoteBtn: { 
        tap: "onNewNote" 
        } 
        } 
        }, 
        onNewNote: function() { 
var values = Ext.getCmp('form').getValues(); 
       console.log("inside onNewNote function"); 

     Ext.Ajax.request({ 
         url: 'http://call.com/the_webservice', 
         params : values, 

         failure: function (response) { 
         var text = response.responseText; 
         console.log("fail"); 

         },        success: function (response) { 
         var text = response.responseText; 
         console.log("success"); 

         } 

         }); 



        } 

        // init and launch functions omitted. 
        }); 

回答

1

1.)在我的代碼中,我只能顯示JSON響應e從VIEW中發出,但我怎樣才能將它發送給COntroller?

爲什麼要將值從controller傳遞迴view

我沒有看到任何有效的理由這樣做。

您可以顯示在控制器本身success像這樣的Web服務的failureExt.Msg.alert

..... 
..... 
url: 'http://call.com/the_webservice', 
params : values, 

failure: function (response) { 
    var text = response.responseText; 
    Ext.Msg.alert('Error','Error while executing Web Service'); 
}, 

success: function (response) { 
    var text = response.responseText; 
    Ext.Msg.alert('Success','Web Service code successfully executed'); 
}, 
..... 
..... 

2)我怎麼可能,字符串從視圖中分離出來,只能得到 來自JSON響應的SUCCESS或FAIL字符串?

編輯:

做這樣的事情..

var result = Ext.decode(response.responseText); 

// result.value = SUCCESS or FAIL 
Ext.Msg.alert('Message',result.value); 
+0

1)的 '控制器' 通常使用的程序(保存,刪除,編輯功能等的執行功能..),'視圖'用於顯示警報(UI組件給用戶)。這是我創建的登錄屏幕,因此根據JSON響應成功/失敗,如果登錄成功或失敗(顯示註冊頁面),我需要提醒用戶。 2.)我正在爲iPhone和Android創建此應用程序。 JSON將包含2個響應中的一個{「value」:「SUCCESS」}或{「value」:「FAIL」}。所以我需要一種方法來獲取JSON響應的'value'屬性的值。 –

+0

1)我沒有看到任何理由只是回電視到顯示彈出窗口。 2)在訪問它的'value'屬性之前,你需要將'Json'字符串解碼爲'Json'對象。 –

+0

檢查答案的編輯部分以顯示適當的響應 –

相關問題