简体   繁体   English

离子:从 http 调用中获取数据

[英]Ionic: Getting data from http calls

I am new to ionic so please be patient if its pretty basic question.我是 ionic 新手,所以如果它是非常基本的问题,请耐心等待。

i am using Advance http plugin for API calls as below我正在为 API 调用使用 Advance http 插件,如下所示

Installation :安装

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

Imports in app.module.ts and work file tab1.page.ts在 app.module.ts 和工作文件 tab1.page.ts 中导入

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

Included provider in app.moudle.ts app.moudle.ts 中包含的提供程序

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

Html page Html页面

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

TS file 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());
  }
 }

Below is my ticket-details.service file that gets data and returns it:下面是我的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;
    });
  }
}

This returns me [object Promise] in alert这会在警报中返回我[object Promise]

if I hit the link in browser I get below如果我点击浏览器中的链接,我会在下面

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

Can someone help me what do i have to fix here to get the message text instead有人可以帮我我必须在这里解决什么来获取消息文本吗

Did you try with "return await" in your getDetails function?您是否尝试在 getDetails function 中使用“return await”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM