我有一个登录页面,用于验证用户。正确的用户名和密码应该隐藏登录视图并显示下一个视图。当我调用该函数执行相同操作时,它会显示错误"Uncaught TypeError: this.functionname is not a function
。是我的相同代码:
startMain : function() {
Eits.store.DataStore.init();
Eits.Factory.getAppController().start();
},
doLoginClicked : function() {
console.log("button pressed this time");
var name = this.view.getUsername();
var pass = this.view.getPassword();
console.log(name);
console.log(pass);
Ext.Ajax.request({
url: 'login/getLoginStatus.action',
method:'post',
params: {
user: name,
password: pass
}
,
success: function(response) {
console.log(response);
var obj = Ext.decode(response.responseText);
var nextObj=obj.data;
console.log(nextObj);
if (nextObj[0].success) {
console.log(nextObj[0].msg);
this.startMain();//Uncaught TypeError: this.startMain is not a function
this.view.hide();//
}
else
{
console.log(nextObj[0].msg);
}
},
failure: function(response,options,success) {
console.log("inside failure");
console.log(response);
console.log(options);
console.log(success);
Ext.Msg.show({
title:'Error!',
msg: response.text,
icon: Ext.Msg.ERROR,
buttons: Ext.Msg.OK
});
}
});
}
}
有没有办法调用这些功能并绕过它?
答案 0 :(得分:2)
范围问题。在成功回调中调用this
时,您不在定义startMain
函数的同一范围内。
一种解决方案是在doLoginClicked
函数内声明对正确范围的引用:
doLoginClicked : function () {
var me = this;
console.log("button pressed this time");
...
然后在Ajax成功回调中,您可以将this
替换为me
:
me.startMain();