这就是验证的方式。 POST https://DomainName/api/v1/authentication?username= {username}&password = {password}。 这是打字稿验证调用代码
authenticate(url, payload){
let username: string = 'admin';
let password: string = 'password';
let headers: Headers = new Headers();
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "application/json");
headers.append("Access-Control-Allow-Origin", "*");
return this.http.post(server + url + addon, payload, { headers:headers });
这是表单数据的代码
// Getting data info from signing form
loginForm = new FormGroup({
username: new FormControl(null, ([Validators.required, Validators.minLength(5)])),
password: new FormControl(null, ([Validators.required, Validators.minLength(5)])),
})
用户数据
// user data
loginData={
username:'',
password:'',
}
buildLoginData(){
this.loginData.password = this.loginForm.controls['password'].value;
this.loginData.username = this.loginForm.controls['username'].value;
}
登录功能
signin(){
this.loading =this.loadingCtrl.create({
content:"Verifying Credentials ..."
});
this.loading.present();
this.buildLoginData();
console.log(this.loginData);
this.http.store('authentication', this.loginData).subscribe((response)=>{
// console.log(response);
// console.log("*******************************************");
// console.log(response['username']);
localStorage.setItem('office_ID',response['officeId']);
localStorage.setItem('user_ID', response['userId']);
localStorage.setItem('username', response['username']);
this.loading.dismissAll();
this.navCtrl.push(HomePage);
},error=>{
this.loading.dismissAll();
if(error.status === 401){
this.toastr.messenger('Wrong Username or Password');
}else
this.toastr.messenger('Login Failed!');
console.log(JSON.stringify(error));
alert("Error : " + error );
});
}
响应日志数据
{"_body":"{\"developerMessage\":\"Invalid authentication details were
passed in api request.\",\"httpStatusCode\":\"401\",\"defaultUserMessage
\":\"Unauthenticated. Please login.\",\"userMessageGlobalisationCode
\":\"error.msg.not.authenticated\",\"errors
\":[]}","status":401,"ok":false,"statusText":"Unauthorized","headers":
{"Content-Type":["application/json"]},"type":2,"url":"https//x.x.x.x:xxxx/xxxxxxxxx/api/v1/authentication?tenantIdentifier=default"}
答案 0 :(得分:0)
标题在Angular中是不可变的,因此,在您的代码中,标题从未真正离开客户端。
这里是解决方法:
let headers: Headers = new Headers();
headers = headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers = headers.append("Content-Type", "application/json");
headers = headers.append("Access-Control-Allow-Origin", "*");
我希望这会有所帮助!