1

誰かが私を助けてくれることを願っています。JSONWebサービスで実装された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 に答える