来自json的sapui5数据
从json读取数据时出现问题。我的login.json文件来自json的sapui5数据,json,cordova,sapui5,Json,Cordova,Sapui5,从json读取数据时出现问题。我的login.json文件 { "Users": [ { "login" : "admin", "password" : "admin" } ] } 这是我的login.xml.view <core:View controllerName="sap.ui.demo.myFiori.view.login" xmlns="sap.m" xmlns:l="sap.ui.layou
{
"Users": [
{
"login" : "admin",
"password" : "admin"
}
]
}
这是我的login.xml.view
<core:View
controllerName="sap.ui.demo.myFiori.view.login"
xmlns="sap.m"
xmlns:l="sap.ui.layout"
xmlns:core="sap.ui.core" >
<Page
title="{i18n>LoginIn}">
<VBox
class="marginBoxContent" >
<items>
<Label text="User" />
<Input
id="nameInput"
type="Text"
placeholder="..." />
<Label text="password" />
<Input
id="passwInput"
type="Password"
placeholder=" ..." />
<Text id="description" class="marginOnlyTop" />
<Button text="Login" press="handleContinue" />
</items>
</VBox>
</Page>
</core:View>
这个登录屏幕可以工作,但我无法从json文件中获取登录名和密码,目前这些数据来自if语句,这不好。有什么建议吗?首先,我假设这只是一个测试应用程序,因为在客户端应用程序中实际拥有正确的纯文本身份验证凭据是相当糟糕的做法 您的问题的关键似乎是:“如何从文件中访问JSON数据?” 关于这个主题,已经有一个问答:但是如果你想使用一个模型,这是UI5应用程序中的常见做法,那么你可以: 1) );JSONModel机制将负责加载;将其指定给视图
this.getView().setModel(new sap.ui.model.json.JSONModel('login.json'), "login");
2) 需要检查时,使用getData()方法访问JSON模型中的数据
(我正在为0个用户编制索引,因为您的authinfo似乎在一个数组中)也许我以前定义了一些错误,但现在它可以工作了。我更改了login.controller.js。现在看起来是这样的:
jQuery.sap.require("sap.ui.demo.myFiori.util.Formatter");
sap.ui.controller("sap.ui.demo.myFiori.view.login", {
onInit: function () {
var oModels = new sap.ui.model.json.JSONModel("login.json");
sap.ui.getCore().setModel(oModels);
},
handleContinue : function (evt) {
var authinfo = this.getView().getModel().getData().Users[0];
var name = this.getView().byId("nameInput").getValue();
var paswd = this.getView().byId("passwInput").getValue();
if (name == authinfo.login && paswd == authinfo.passw) {
var context = evt.getSource().getBindingContext();
this.nav.to("Master", context);
}
else {
jQuery.sap.require("sap.m.MessageToast");
sap.m.MessageToast.show("Wrong login");
}
}
});
似乎您从未将json数据加载到模型中—也许这是一个测试应用程序;不要这样做,这是非常糟糕的做法,而且不安全。获取此错误:Uncaught TypeError:无法读取未定义的属性“0”。有什么想法吗?你做了步骤1中的事情吗?然后我会做一些调试,找出命名模型中的内容,然后从那里开始。
// Note - extremely bad practice - don't do this for real!
var authinfo = this.getView().getModel("login").getData().Users[0];
if (name == authinfo.login && paswd == authinfo.password) {
....
jQuery.sap.require("sap.ui.demo.myFiori.util.Formatter");
sap.ui.controller("sap.ui.demo.myFiori.view.login", {
onInit: function () {
var oModels = new sap.ui.model.json.JSONModel("login.json");
sap.ui.getCore().setModel(oModels);
},
handleContinue : function (evt) {
var authinfo = this.getView().getModel().getData().Users[0];
var name = this.getView().byId("nameInput").getValue();
var paswd = this.getView().byId("passwInput").getValue();
if (name == authinfo.login && paswd == authinfo.passw) {
var context = evt.getSource().getBindingContext();
this.nav.to("Master", context);
}
else {
jQuery.sap.require("sap.m.MessageToast");
sap.m.MessageToast.show("Wrong login");
}
}
});