首页 > 解决方案 > 在 Ionic 中使用 API 执行登录时出现 JSON 错误

问题描述

我正在使用 Ionic 中的 API 执行登录,但出现错误:

错误:类型“{}”上不存在属性“json”。

这是我的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>

这是我的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) {
      this.getloginUsers();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginpagePage');
  }

    getloginUsers(){
    this.restProvider.getUsers(this.userData,'user_Login').then((result) => {
      if(result){
       this.responseData = result.json();
     if(this.responseData.userData){
     console.log(this.responseData);
     console.log("User Details");
     this.navCtrl.push(ListPage);
     }
     else{
       console.log("Incorrect Details"); }
    }
     }
     , (err) => {
     // Error log
   });

 }
}

这是代码this.responseData = result.json();错误即将到来。

错误:类型“{}”上不存在属性“json”。

这是我的服务 restapi.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout'

let apiUrl = 'http://192.168.1.10/honeybee/HoneyApi/';

@Injectable()
export class RestapiProvider {

  token:any;

  constructor(public http: HttpClient) {
    console.log('Hello RestapiProvider Provider');
  }


  getUsers(credentials, type) {
    return new Promise((resolve, reject) => {
      var headers = new HttpHeaders();
      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: Response) => {
        resolve(res);
      }, (err) => {
          reject(err);
        });
    });
  }
}

我在 app.module.ts 中包含了 FormsModule。任何帮助深表感谢。

标签: htmljsonangularionic-frameworkionic3

解决方案


由于您使用的是 HttpClient,因此您通常不必使用result.json();

 this.responseData = result;

此外,您不必使用 Promise,将服务代码更改如下,

 getUsers(credentials, type) {
      var headers = new HttpHeaders();
      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');
      return this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers});
  }

在你的组件中,

this.restProvider.getUsers(this.userData,'user_Login').subscribe((data) => {
    console.log(data);
});

推荐阅读