我是Angular和ionic的新手。这是我的第一个项目。我想从角度调用API端点,并在有效响应上想切换到下一页。
它不起作用,我遇到了这个错误
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "OK", url: "http://api/auth/session", ok: false, …}
我已经搜索了几乎互联网上的所有内容,但找不到任何解决方案。请帮助我。
代码:
对于我的登录文件:
import { Component } from '@angular/core';
import { IonicPage, NavController, LoadingController } from 'ionic-angular';
import { DashboardPage } from '../dashboard/dashboard';
import { AuthProvider } from '../../providers/auth/auth';
import { Storage } from '@ionic/storage';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
constructor(public navCtrl: NavController,
public loadingController: LoadingController,
public authService: AuthProvider,
public storage: Storage) {
}
auth() {
}
dashboard() {
this.authService.loginUser("test1", "1234").subscribe((result) =>{
console.log(`result :: ${JSON.stringify(result)}`);
if(result.sessionToken){
this.set('token', result.sessionToken);
}
this.navCtrl.setRoot(DashboardPage)
})
}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginPage');
this.auth();
}
public set(settingName, value){
return this.storage.set(`setting:${ settingName }`,value);
}
async presentLoading() {
const loading = await this.loadingController.create({
duration: 2000
});
return await loading.present();
}
}
auth.js
@Injectable()
export class AuthProvider {
constructor(public http: HttpClient) {
console.log('Hello AuthProvider Provider');
}
loginUser (email : string, password : string) : Observable <any> {
const headers = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
return this.http.post <any> ('http://api/auth/session',{
"username": email,
"password":password
}, headers);
}
}
这是我的全部代码。请帮忙。