2013-12-19 167 views
4

我想打開新窗口中的json響應,我嘗試了很多,但沒有得到ant成功,如果有人能幫助我,將不勝感激。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"); 
    } 
}); 

我在二是新的,如果任何機構可以幫助我將不勝感激,並提前感謝百萬噸。

回答

1

您的窗口對象被創建,但它是空的,並具有透明背景,所以你不會在屏幕上看到它。

你可以改變你的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,並在每次保存時檢查您的代碼。

+0

感謝兄弟,它的工作現在我明白我在哪裏會犯錯誤。 –