2013-02-06 26 views
0

我想創建一個登錄表單,我發佈用戶名和密碼到服務器,並獲得響應處理徹底的Ext.Ajax調用。下面是我的代碼:試圖獲得響應從服務器與sencha的Ext.Ajax調用2

我的登錄表單

Ext.define('GS.view.Main', { 
    extend: 'Ext.form.Panel', 
    config: { 
     fullscreen: true, 
     id: 'register', 
     frame:true, 
     items: [{ 
      xtype: 'fieldset', 
      items: [ 
       { 
        xtype: 'textfield', 
        name : 'userName', 
        itemId: 'userName', 
        placeHolder : 'Orgnisationsnummer', 
       }, 
       { 
        xtype: 'passwordfield', 
        name : 'password', 
        itemId: 'password', 
        placeHolder : 'Lösenord' 
       } 
      ] 
     }, { 
      xtype: 'button', 
      text: 'Logga in', 
      minHeight: '45px', 
      action: 'submitFormAction' 
     }] 
    } 
}); 

我控制器

Ext.define('GS.controller.Controller', { 
    extend : 'Ext.app.Controller',  
    config : { 
     control : { 
      'button[action="submitFormAction"]' : { 
       tap : 'submitForm' 
      }, 
     } 
    }, 
    submitForm : function() { 
     var form = Ext.getCmp('register'); 
     Ext.Ajax.request({ 
      url : "URL HERE", 
      params : { username : form.getValues().userName, password: form.getValues().password }, 
      method: 'POST', 
      success: function (result, request) { 
       console.log(result); 
       var jsonData = Ext.decode(result.responseText); 
       Ext.Msg.alert('Success', 'Data return from the server: '+ jsonData.msg); 
      }, 
      failure: function (result, request) { 
       Ext.Msg.alert('Failed', result.responseText); 
      } 


     }); 
    } 
}); 

我簡單的PHP腳本

<?php 
$username = $_POST["userName"]; 
$password = $_POST["password"]; 



if(!empty($username) && !empty($password)) { 
    $response = array("success" => 'true', 'id' => '1'); 
    echo json_encode($response); 

} 

?> 

問題是我從中得到響應發球r:

getAllResponseHeaders: function() { 
getResponseHeader: function (header) { 
request: Object 
requestId: 1 
responseText: "" 
responseXML: null 
status: 200 
statusText: "OK" 
__proto__: Object 

Error: You're trying to decode an invalid JSON String: 

這是怎麼回事?我究竟做錯了什麼?我是否沒有正確發佈表單值?

謝謝!

/福利局的:)

回答

1

您發佈兩個參數:

  • 「用戶名」
  • 「密碼」

...但在你的PHP腳本,你'尋找這兩個:

  • 「user名稱」(注意大寫N)
  • ‘密碼’

我想這可能是問題,但我不明白你在PHP中,這樣的錯誤消息是有點奇怪解碼東西。

我的建議是迴應PHP的用戶名/密碼值,以確保它們正確發送/接收。您也可以在您的JS代碼中設置一些斷點,以確保在發送AJAX請求之前這些值是正確的。當我不知道你實際發送了什麼樣的價值時,很難說一切都很好。

+0

啊,昨晚錯過了!謝謝! – Ismailp

相關問題