Icce___ 2019-06-22 13:53 采纳率: 66.7%
浏览 598
已采纳

EXTJS中ajax请求回调函数中如何跳转页面?

初学extjs,网上找了个登录功能的案例,我的理解是填写表单点击按钮就直接给全局变量(是否登录)赋值,然后将当前登录的窗口删除,重新创建主页面的对象。

    onLoginClick: function(){
        // Set the localStorage value to true
        localStorage.setItem("isLogin", true);
        // Remove Login Window
        this.getView().destroy();
        // Add the main view to the viewport
        Ext.create({
            xtype: 'app-main'
        });
    },

这是没有与后端结合的,我就给他加了个向后端发送请求,然后实现账号密码正确就跳转页面,错误就弹窗提示。

    loginChange: function(){
        // Set the localStorage value to true
        localStorage.setItem("isLogin", true);
        // Remove Login Window
        this.getView().destroy();
        // Add the main view to the viewport
        Ext.create({
            xtype: 'app-main'
        });
    },
    onLoginClick: function(){
        Ext.Ajax.request({
            url:"/openmaint/services/json/session/create",
            method:"POST",
            params: {
                username: Ext.getCmp('username').getValue(),
                password: Ext.getCmp('password').getValue()
             },
            success: function(response, options){
                var respText = Ext.util.JSON.decode(response.responseText);
                if(respText.success){
                    this.loginChange();
                    console.log(respText.response.group);
                }else{
                    Ext.Msg.alert("提示","账号或密码错误!")
                }
            }
        });

但是这样写的话,会在this.loginChange();这一行报错,Uncaught TypeError: this.loginChange is not a function。
如果调换这行代码的位置,放到request方法外面,是不会报错的。所以是不是在request方法里这个this对象并不是当前页面这个对象?这种情况下该如何修改才能实现我想要的效果?location.href这个方法我试过,可以行得通,但是这样的话就得有两个页面(html文件)。不太希望使用这种方法。请大家帮忙解答下,十分感谢!!!

  • 写回答

1条回答 默认 最新

  • Icce___ 2019-06-22 18:06
    关注

    问题解决了,跟ajax请求无关,在其他地方也碰到了同样的问题。主要是js在嵌套的方法里,this对象会被覆盖,所以导致找不到方法。只要在外一层的方法里面先把this赋值给另一个不会重名的变量就可以了。

    ```onLoginClick: function(){
    var that=this;
    Ext.Ajax.request({
    url:"/openmaint/services/json/session/create",
    method:"POST",
    params: {
    username: Ext.getCmp('username').getValue(),
    password: Ext.getCmp('password').getValue()
    },
    success: function(response, options){
    var respText = Ext.util.JSON.decode(response.responseText);
    if(respText.success){
    that.loginChange();
    console.log(respText.response.group);
    }else{
    Ext.Msg.alert("提示","账号或密码错误!")
    }
    }
    });

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题