javascript - json 响应未在新窗口中打开

标签 javascript titanium titanium-mobile

我想在新窗口中打开 json 响应,我尝试了很多但没有成功,如果有人能帮助我,我将不胜感激。

这是我的 app.js 代码

Titanium.UI.setBackgroundColor('#fff');
var tabGroup = Titanium.UI.createTabGroup();

var login = Titanium.UI.createWindow({
    title:'User Authentication Demo',
    tabBarHidden:true,
    url:'main_windows/login.js'
});

var loginTab = Titanium.UI.createTab({
    title:"Login",
    window:login
});

tabGroup.addTab(loginTab); 
tabGroup.open();

这是我的 login.js 代码

var win = Titanium.UI.currentWindow;

var UserLogin = Titanium.UI.createTextField({
    color:'#336699',
    top:10,
    left:10,
    width:300,
    height:40,
    hintText:'UserLogin',
    keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
    returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(UserLogin);

var UserPassword = Titanium.UI.createTextField({
    color:'#336699',
    top:60,
    left:10,
    width:300,
    height:40,
    hintText:'UserPassword',
    passwordMask:true,
    keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
    returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(UserPassword);

var loginBtn = Titanium.UI.createButton({
    title:'Login',
    top:110,
    width:90,
    height:35,
    borderRadius:1,
    font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
});
win.add(loginBtn);

/*
* Login Event Handling
*/
var loginReq = Titanium.Network.createHTTPClient();
var data, User, UserName, UserEmail, UserFirstName, UserLastName, UserAvatar, BioGraphyNative,BioGraphyEnglish,UserPhone,CreatedOn;
loginReq.onload = function() {
    var json = this.responseText;
    var response = JSON.parse(json);
    var message = response.message;
    var code    = response.code;

    if (response.data == null) {
        alert("Message " + message + "code " + code);
    } else {
        var win1 = Titanium.UI.createWindow();
        win1.open();

        User               = response.data.User;
        UserName           = User.UserLogin;
        UserEmail          = User.UserEmail;
        UserFirstName      = User.UserFirstName;
        UserLastName       = User.UserLastName;
        UserAvatar         = User.UserAvatar;
        BioGraphyNative    = User.BioGraphyNative;
        BioGraphyEnglish   = User.BioGraphyEnglish;
        UserPhone          = User.UserPhone;
        CreatedOn          = User.CreatedOn;
        alert("UserName " + UserName + "UserEmail " + UserEmail);
    }
};

loginReq.onerror = function() {
    alert("Network error");
};

/*
* Login Button Click Event
*/

loginBtn.addEventListener('click',function(e) {

    if (UserLogin.value !== '' && UserPassword.value !== '') {
        var win1 = Titanium.UI.createWindow();
        loginReq.open("POST", "url");
        var params = {
            UserLogin: UserLogin.value,
            UserPassword: UserPassword.value
        };
        loginReq.send(params);

    } else {
        alert("Username/Password are required");
    }
});

我是钛新手,如果有任何人可以帮助我,我将不胜感激,并提前致谢。

最佳答案

您的窗口对象已创建,但它是空的且具有透明背景,因此您在屏幕上看不到它。

你可以把你的onload改成这样:

loginReq.onload = function() {
    var json = this.responseText;
    var response = JSON.parse(json);
    var message = response.message;
    var code    = response.code;

    if (response.data == null) {
        alert("Message " + message + "code " + code);
    } else {
        var win1 = Titanium.UI.createWindow({
            backgroundColor: 'white',
            layout: 'vertical',
        });

        var User = response.data.User;
        for (var i in User) {
            win1.add(Ti.UI.createLabel({ text: User[i] });
        }
        win1.open();
    }
};

还要注意 JavaScript 问题,例如在没有 var 语句的情况下声明变量。始终使用并尝试将所有声明放在函数的开头。为了对其进行监控,最好安装 JSHint 或 JSLint 并在每次保存时检查您的代码。

关于javascript - json 响应未在新窗口中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680827/

相关文章:

javascript - 使用网络移动框架?

javascript - 从变量动态创建命名空间对象

javascript - Titanium 音乐播放器进度事件监听器状态未触发

javascript - 将事件监听器传递给事件监听器?

javascript - Titanium Studio javascript - 添加到数组

javascript -  '{ title: string; description: string; }' 类型的参数不可分配给  '{ id: {}; title: {}; description: {}; }' 类型的参数

javascript - phantomjs 总是在 Windows 上给出解析错误

javascript - jQuery onfocus 和 onblur 事件缩小

javascript - 使用 Asp.net web api 下载时出错

javascript - Web 应用程序中的 Mac 多点触控