EXTJS 5.1 为登录屏幕匹配 json 值

EXTJS 5.1 Matching json values for log in screen

我已经为我的登录屏幕构建了一个控制器:

    Ext.define('ies.view.login.LoginController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.login',
    onLoginClick: function(){

        //get form field values
        var username = this.lookupReference('username').getValue();
        var password = this.lookupReference('password').getValue();
        var LoginError = document.getElementById("displayfield-1014-inputEl");
        //console.log(username + ' ' + password);



// set usernames and a single passwords
if ((username === 'carol' || username === 'denise' || username === 'coley' || username === 'yegappan' || username === 'julie' || username === 'dawn' || username === 'yvonne' || username === 'chuck' || username === 'belinda' || username === 'atlante' || username === 'blake' || username === 'ernie' || username === 'Patrick.Dwyer') && password === 'password1'){

        // Set the localStorage value to true
        localStorage.setItem("TutorialLoggedIn", true);
        localStorage.setItem(username, true);
        //console.log("Authenticated");

        // Remove Login Window
        this.getView().destroy();

        // Add the main view to the viewport
        Ext.widget('app-main');
    } else {


        LoginError.innerHTML = "Wrong Username or Password";
        LoginError.style.color = "#ff0000";
        console.log("Not authenticated");
    }
    }
});

效果很好,但我正在尝试从 json 文件中获取身份验证值:

[
    {
        "username":"angel",
        "password":"password1"
    },
    {
        "username":"chuck",
        "password":"password1"
    },
    {
        "username":"yvonne",
        "password":"password1"
    },
    {
        "username":"belinda",
        "password":"password1"
    },
    {
        "username":"patrick",
        "password":"password1"
    },
    {
        "username":"carol",
        "password":"password1"
    }
]

以下代码获取 ajax 请求并发出警报以显示用户名。我正在让它工作,但我未定义为用户名:

Ext.Ajax.request({
  url: 'app/model/cred.json',
  params: {
    someParam: 'someValue'
  },
  method: 'POST',
  success: function(result, request) {
    var json = Ext.decode(result.responseText);
    Ext.Msg.alert('JSON Data', 'Username is: ' + json.username);       
  },
  failure: function(result, request) {
    Ext.Msg.alert('Error', 'An Error has bitten ye squarely on the keister...');
  } 
});

我想问题是如何获取 json 文件的值,如果用户名和密码匹配,设置允许用户登录应用程序的本地存储...我我被困在如何处理这个问题上——我什至无法在警报中显示 json 文件的值...

这是一个 fiddle,只有 ajax 请求:https://fiddle.sencha.com/#fiddle/ift

非常感谢所有帮助!

我认为这里的关键是查看 var json 值。将您的回调代码更改为以下,您将在消息框中看到 username 值。

Ext.Msg.alert('JSON Data', 'Username is: ' + json[0].username);