1

我希望有人能帮助我。我有一个使用 JSON Web 服务实现的 API。我想实现登录。创建了一个用户,我需要登录该用户。那就是当我输入用户名和密码时,它必须让用户登录。

我已阅读tutsplus教程,但无法验证用户身份。谁能帮我吗。

这是我正在使用的代码:

// create tab group
var tabGroup = Titanium.UI.createTabGroup();

var win1 = Titanium.UI.createWindow({  
title:'Login',
backgroundColor:'#fff'
});

var username = Ti.UI.createTextField({
top:'10%',
borderRadius:3,
hintText:'username',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
width:'80%',
height:'auto',
left:'10%',
right:'10%',
touchEnabled: true, 
});
win1.add(username);
var pass = Ti.UI.createTextField({
top:'30%',
borderRadius:3,
hintText:'password',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
width:'80%',
height:'auto',
left:'10%',
right:'10%',
touchEnabled: true,
passwordMask: true  
});

win1.add(pass);
var loginBtn = Titanium.UI.createButton({  
title:'Login',  
top:'50%',  
width:'60%',  
height:'15%',  
borderRadius:1,  
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}  
});  
win1.add(loginBtn);

var url = 'http://qudova.com/api.php?function=AuthenticateUser&u=ns.nadeem.m@gmail.com&p=qudovatest';
var json;
var loginReq = Titanium.Network.createHTTPClient();  
loginBtn.addEventListener('click',function(e)  
{  
if (username.value != '' && pass.value != '')  
{  
    // Here I will get the Token (asdfasdf....)
    loginReq.open("GET",url);  
    authstr = 'Basic ' +Titanium.Utils.base64encode(username.value +':' +pass.value); 
    loginReq.setRequestHeader('Authorization', authstr);

    loginReq.send();  
}  
else  
{  
    alert("Username/Password are required");  
}  
});
loginReq.onload = function()  
{  
    var jsonObject = JSON.parse(this.responseText);
    // Here I have made a check if the Token is returned successfully it will alert the user that he authenticated 
    if (jsonObject.Token == "asdfadsfasdfadsf")  
        {  
            alert("Authenticated");  
        }  
    else  
        {  
            alert("response.message");  
        }  
}; 
win1.open();

提前致谢。我的概念清楚吗?

4

1 回答 1

2

你得到一个 JSON 数组作为响应。所以你应该访问jsonObject[0].Token.

// create tab group
var tabGroup = Titanium.UI.createTabGroup();

var win1 = Titanium.UI.createWindow({  
title:'Login',
backgroundColor:'#fff'
});

var username = Ti.UI.createTextField({
top:'10%',
borderRadius:3,
hintText:'username',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
width:'80%',
height:'auto',
left:'10%',
right:'10%',
touchEnabled: true, 
});
win1.add(username);
var pass = Ti.UI.createTextField({
top:'30%',
borderRadius:3,
hintText:'password',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
width:'80%',
height:'auto',
left:'10%',
right:'10%',
touchEnabled: true,
passwordMask: true  
});

win1.add(pass);
var loginBtn = Titanium.UI.createButton({  
title:'Login',  
top:'50%',  
width:'60%',  
height:'15%',  
borderRadius:1,  
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}  
});  
win1.add(loginBtn);

var url = 'http://qudova.com/api.php?function=AuthenticateUser&u=ns.nadeem.m@gmail.com&p=qudovatest';
var json;
var loginReq = Titanium.Network.createHTTPClient();  
loginBtn.addEventListener('click',function(e)  
{  
if (username.value != '' && pass.value != '')  
{  
    // Here I will get the Token (asdfasdf....)
    loginReq.open("GET",url);  
    authstr = 'Basic ' +Titanium.Utils.base64encode(username.value +':' +pass.value); 
    loginReq.setRequestHeader('Authorization', authstr);

    loginReq.send();  
}  
else  
{  
    alert("Username/Password are required");  
}  
});
loginReq.onload = function()  
{  
    var jsonObject = JSON.parse(this.responseText);
    // Here I have made a check if the Token is returned successfully it will alert the user that he authenticated 
    if (jsonObject[0].Token === "asdfadsfasdfadsf")  
        {  
            alert("Authenticated");  
        }  
    else  
        {  
            alert("response.message");  
        }  
}; 
win1.open();

或者,您可以更改后端实现,结果将是一个对象而不是一个数组。

尽管如此,您应该更改您的后端,因为目前可以通过普通的 GET 参数进行身份验证。

于 2013-03-18T12:02:15.980 回答