2012-10-08 103 views
2

我正在创建登录系统Sencha Touch 2。我在提交表单时遇到问题。它没有从服务器获取响应数据。下面是我的代码表单提交不起作用

控制器:

Ext.define("MyMobile.controller.LoginController", { 
extend: "Ext.app.Controller", 
views: ['LoginView'], 

config: { 
    refs: { 
     loginForm: "#loginFormPanel" 
    }, 
    control: { 
     'button[action=login]': { 
      tap: "authenticateUser" 
     } 
    } 
}, 

authenticateUser: function (button) { 

    this.getLoginForm().submit({ 
     url: 'login/authenticate', 
     method: 'POST', 
     success: function (form, result) { 
      debugger; //This block of code is not executing even after JSON response 
      var jsonoutput = Ext.decode(result); // json parsing 
      Ext.MessageBox.alert('Error', "Success"); 

     }, 
     failure: function (form, result) {//This block of code is not executing even after JSON response 
      Ext.MessageBox.alert('Error', "Invalid username/password"); 
     } 

    }); 
} 

}); 

查看

Ext.define("MyMobile.view.LoginView", { 
extend: "Ext.form.FormPanel", 
alias: "widget.mylogin", 
id: 'loginFormPanel', 

config: { 

    margin: '0 auto', 
    name: 'loginform', 
    frame: true, 
    url: 'login/Authenticate', 
    title: 'Login', 
    items: [ 
     { 
     xtype: 'fieldset', 

     itemId: 'LoginFieldset', 
     margin: '10 auto 0 auto ', 

     title: '', 
     items: [ 
       { 
        xtype: 'textfield', 

        label: 'User Name', 
        name: 'my-username', 
        required: true, 

        placeHolder: 'Username' 
       }, 
       { 
        xtype: 'emailfield', 
        label: 'Email', 
        name: 'Email' 
       }, 
       { 
        xtype: 'passwordfield', 

        label: 'Password', 
        name: 'my-password', 
        required: true, 

        placeHolder: 'Password' 
       } 
      ] 
    }, 
    { 
     xtype: 'button', 
     id: 'loginButton', 
     margin: '25 auto 0 auto ', 
     style: '', 
     maxWidth: 200, 
     ui: 'action', 
     width: '', 
     iconCls: 'user', 
     iconMask: true, 
     text: 'Login', 
     action: 'login' 
    } 

    ] 
} 

}); 

App.JS

Ext.application({ 

name: "MyMobile", 
appFolder: "myapp", 
controllers: ["LoginController"], 
views: ['LoginView'], 

launch: function() { 

    var loginPanel= Ext.create('Ext.Panel', { 
     layout: 'fit', 

     items: [ 
      { 
       xtype: 'mylogin' 
      } 
     ] 
    }); 


    Ext.Viewport.add(loginPanel); 
} 

}); 

可不可以有一个人凑ld弄清楚应该是什么问题?

下面是我从服务器获得的JSON响应。

{ “用户名”: “穆拉利”, “isAdmin”:真实的, “isAuthenticated”:真正}

即使得到一个JSON和200确定结果后,我的代码提交表单功能进入衰竭回调。在失败回调函数失败:函数(形式,结果)我得到的结果参数为我的JSON。但为什么它失败了?

+0

“下面是来自我的服务器的JSON响应。“,所以你得到了一个结果,或者这是你期望得到的结果吗? – Rob

+0

我得到JSON响应和200好结果。但是表单提交的成功或失败回调没有执行 –

+1

嗯,如果你使用'callback:function(){console.log('cb'); }'? – Rob

回答

2

让你的服务器返回JSON响应象下面这样:

如果成功:

{ 
    "success":true, 
    "UserName":"Murali", 
    "isAdmin":true, 
    "isAuthenticated":true 
} 

如果失败:

{ 
    "success":false 
} 

阅读:http://docs.sencha.com/touch/2-0/#!/api/Ext.form.Panel-method-submit

+0

Hi Sreenath。真棒!它像魅力一样工作!谢谢。是否有可能将我的cookie值作为响应头的一部分发送并将其用于后续请求?像这样,我们在正常的网络上进行会话管理 –

+1

yes ofcourse它的可能性,请阅读:http://stackoverflow.com/a/5045117/1600692或http://stackoverflow.com/questions/8733025/setting -persistent-cookies-with-javascript,你可以在你的'success'方法中做到这一点。 –

+0

感谢您的参考。我正在通过http://stackoverflow.com/questions/12384150/authentication-on-sencha-touch-and-remote-server?lq=1。同样也会让人感到困惑,比如如何将cookie存储在本地存储中,并在每个请求中将其作为头参数进行检索和发送。有没有例子? –