9 authentication api http ionic3 angular
我正在使用API在Ionic 3中进行身份验证但在登录过程中,它显示错误:无法读取null的属性'json'
这是我的提供者> restapi> restapi.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
let apiUrl = 'http://192.168.1.10/honeybee/HoneyApi/';
@Injectable()
export class RestapiProvider {
constructor(public http: HttpClient) {
console.log('Hello RestapiProvider Provider');
}
getUsers(credentials, type) {
return new Promise((resolve, reject) => {
var headers = new Headers();
headers.append('Access-Control-Allow-Origin' , '*');
headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
headers.append('Accept','application/json');
headers.append('content-type','application/json');
this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
.subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的loginpage.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { RestapiProvider } from '../../providers/restapi/restapi';
import { ListPage } from '../list/list';
@IonicPage()
@Component({
selector: 'page-loginpage',
templateUrl: 'loginpage.html',
})
export class LoginpagePage {
responseData : any;
userData = {"email": "", "password": ""};
constructor(public navCtrl: NavController, public navParams: NavParams,
public restProvider: RestapiProvider) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad LoginpagePage');
}
getloginUsers(){
this.restProvider.getUsers(this.userData,'user_Login').then((result) => {
this.responseData = result;
if(this.responseData.userData){
console.log(this.responseData);
console.log("User Details");
this.navCtrl.push(ListPage);
}
else{
console.log("Incorrect Details"); }
}, (err) => {
// Error log
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的loginpage.html
<ion-header>
<ion-navbar>
<ion-title>loginpage</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<form (submit)="getloginUsers()">
<ion-list>
<ion-item>
<ion-label fixed>Email</ion-label>
<ion-input type="email" [(ngModel)]="userData.email" name="email"></ion-input>
</ion-item>
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password" [(ngModel)]="userData.password" name="password"></ion-input>
</ion-item>
<div padding>
<button ion-button color="primary" block>Login</button>
</div>
</ion-list>
</form>
</ion-content>
Run Code Online (Sandbox Code Playgroud)
当我点击登录按钮时,我得到一个无法读取null的属性'json'.在Advance中获得任何帮助.请帮忙.
小智 1
第一的:
如果您FormsModule在配置文件中添加, ngModel就可以工作。
@NgModule({
declarations: [ MyApp ],
imports: [
FormsModule
...
)],
bootstrap: [...],
entryComponents: [ ... ],
providers: []
})
Run Code Online (Sandbox Code Playgroud)
第二:
以JSON格式发送数据,添加Content-Type:
getUsers(credentials, type) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http
.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
.map(res => res.json());
}
Run Code Online (Sandbox Code Playgroud)
并致电loginpage (无承诺)
this.restProvider.getUsers(this.userData,'user_Login')
.subscribe(res => this.responseData = result);
Run Code Online (Sandbox Code Playgroud)
第三:
后端必须返回成功值。如果您的 API 有错误(没有有效的电子邮件、密码),则向客户端返回HTTP错误。
CORS标头必须在服务器部分上实现。
| 归档时间: |
|
| 查看次数: |
518 次 |
| 最近记录: |