问题
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.
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;
data:string;
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".
回答1:
You should do something like this:
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
}
});
来源:https://stackoverflow.com/questions/57175512/how-i-get-validate-the-username-and-password-in-sql-management-using-api