【问题标题】:Ionic: Getting data from http calls离子:从http调用中获取数据
【发布时间】:2021-07-10 10:53:07
【问题描述】:

我是 ionic 新手,所以如果它是非常基本的问题,请耐心等待。

我正在使用 Advance http 插件进行 API 调用,如下所示

安装

ionic cordova plugin add cordova-plugin-advanced-http
npm install @ionic-native/http

在 app.module.ts 和工作文件 tab1.page.ts 中导入

import {HTTP} from '@ionic-native/http/ngx';

app.moudle.ts 中包含的提供者

 providers: [BarcodeScanner,  HTTP,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],

HTML 页面

  <ion-button size="large" expand="block" (click)="tcketDetails()"> Data</ion-button>

TS 文件

import { Component } from '@angular/core';
import {BarcodeScanner,BarcodeScannerOptions} from '@ionic-native/barcode-scanner/ngx';
import {AlertController} from '@ionic/angular';
import {TicketDetailsService} from '../ticket/ticket-details.service';

@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  scannedData: any;
  qrcode_esult: any;
  picture: string;
  constructor(public alertController: AlertController,  private barcodeScanner: BarcodeScanner,private ticekt_details: TicketDetailsService) {}
  tcketDetails(){
    alert(this.ticekt_details.get_ticket_details());
  }
 }

下面是我的 ticket-details.service 文件,它获取数据并返回它:

import { Injectable } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';

@Injectable({
  providedIn: 'root'
})
export class TicketDetailsService {
  private server_path = 'https://websitelink.net/dev/tickets/ticekt_details/1';
  constructor(private http: HTTP) { }

  async get_ticket_details(){
    let postedvalues = {
      'key':'value'
    };
    const optionsValue = {
      'Content-Type': 'application/json',
      'X-API-KEY': ''
    };

    var url=this.server_path;
    return this.http.post(url, postedvalues, optionsValue).then(data => {
      var response=JSON.parse(data.data);
      return response;
    }).catch(error => {
      return error;
    });
  }
}

这会在警报中返回我 [object Promise]

如果我点击浏览器中的链接,我会在下面

{"data":{"status":0,"message":"some message"}}

有人可以帮我解决什么问题才能获取消息文本

【问题讨论】:

标签: angular ionic-framework hybrid-mobile-app


【解决方案1】:

您是否尝试过在 getDetails 函数中使用“return await”?

【讨论】:

  • 什么意思?
  • 对不起,我想我误解了你的问题,并再次阅读。您可以将您的响应放在一个对象中,并将其与您的警报一起显示在您的视图中,因为它是异步模型的承诺......
  • 好的,你能帮我解决一下吗?我提出了这么详细的问题,让我自己累了,它不适合我,谢谢
  • 你能做一个 stackblitz 的例子,这样我就可以玩了。
  • 在这一行: return await this.http.post(url, postedvalues, optionsValue).then(data => {
猜你喜欢
  • 2019-06-03
  • 1970-01-01
  • 2015-02-15
  • 2020-08-07
  • 2018-05-16
  • 2017-12-17
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
相关资源
最近更新 更多