问题描述
我创建了我的登录页面,我想验证用户名和密码.然后我需要使用 API 从数据库中获取用户详细信息.请给我发送一个使用 ionic-3 和 angular-2 的编码.
I created my login page and i want to validate the username and password. Then i need to get the user details using API from the database. please send me a coding using ionic-3 and angular-2.
我已经在我的 SQL 中尝试过了.但我想在 SQL 管理中使用它.在下面的编码中,我使用 post
方法发送参数,它将在后端获取并使用已注册的用户名和密码进行验证.
I already tried in my SQL. But i want it in SQL management. In the below coding i send the parameters using post
method and it will get at back end and validate with already registered username and password.
Username: any;
Password: any;
数据:字符串;
constructor( ){}
//declaration of object//
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json' );
let option = new RequestOptions({headers: headers });
let data = {
username: this.Username.value,
password: this.Password.value
};
let loader = this.loading.create({
spinner: 'bubbles',
duration: 1000
});
//API call//
loader.present().then(() => {
this.http.post('')
.map(res => res.json())
.subscribe(res => {
console.log(res)
loader.dismiss()
if(res=="Your Login Success"){
let alert = this.altCtrl.create({
title:"CONGRATS",
subTitle:(res),
buttons: ['OK']
});
alert.present().then(()=>
this.navCtrl.push(NextPage));
}
else{
let alert = this.altCtrl.create({
title:"ERROR",
subTitle:"Your Login Username or Password is invalid",
buttons: ['OK']
});
alert.present();
}
},(err) => {
});
});
}
我希望如果用户名或密码错误,它应该显示警告用户名或密码不正确".否则它将推送到下一个页面,并显示您的登录成功".
I expect if the username or password is wrong it should show alert "username or password incorrect". in else it will push to the next page with "your login success".
推荐答案
你应该这样做:
在您的 Login.ts 中
in your Login.ts
let loginObject = {
username: userInputValueFromUsername
password: userInputValueFromPassword
}
this.headers = {'Content-Type':'application/json'}; // set Headers for the Request
this.http.post('https://somew-url.com/api/login', JSON.stringify(loginObject), {headers: this.headers})
.subscribe(data => {
console.log(data);
if (data.success == true) {
//push to new Page
}
else{
// Show Error
}
});
这篇关于我如何获得 &使用 API 在 SQL 管理中验证用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!