【发布时间】: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"}}
有人可以帮我解决什么问题才能获取消息文本
【问题讨论】:
-
请观看此视频以了解承诺是什么,我认为它的解释非常清晰:youtube.com/watch?v=RvYYCGs45L4
标签: angular ionic-framework hybrid-mobile-app