Ionic 应用程序无法在 Android 台设备上正常运行

Ionic App not working correctly on Android devices

目前我正在使用 Ionic 中的一个简单应用程序工作,该应用程序从 JSON 文件中检索数据并将其显示在屏幕上。当我 运行 ionic serve/lab 中的应用程序完美运行时,当我尝试模拟它或 运行 在实际 phone 上运行它时,问题就来了。

Not working on the emulator

Working on Ionic Lab/serve

我认为这个问题可能与否 'Access-Control-Allow-Origin' 或与 CORS 的兼容性有关,但我不知道该怎么做才能解决这个问题,所以我很感激你在这方面的知识。

这是我的 .ts 文件代码:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

export class Page4Page {

  datalamp: Array<any> = [];
  datosarr: Array<any> = [];

  constructor(public navCtrl: NavController, public navParams: NavParams, private http: Http) {}

  ionViewDidLoad() {
    this.getdata();
    console.log('ionViewDidLoad Page4Page');
  }

  getdata() {
this.http.get('http://caidalibre.000webhostapp.com/data3.json')
  .map((res) => res.json())
  .subscribe(data => {
    this.datalamp = data;
    console.log(this.datalamp)
    this.getlastdata();
  })
  }
  getlastdata() {

    this.datosarr.length = 0;
    console.log(this.datosarr)
    let lastdata = this.datalamp.slice(-1)[0]
    let lasttemp = JSON.parse(lastdata.temperatura);
    let lasthum = JSON.parse(lastdata.humedad_relativa);
    let lastintesidad = JSON.parse(lastdata.intensidad_luminosa);
    let lastbateria = JSON.parse(lastdata.bateria);
    let lastvoltaje = JSON.parse(lastdata.voltaje_panel);
    this.datosarr.push(lasttemp, lasthum, lastintesidad, lastbateria, lastvoltaje)
    console.log(this.datosarr)

  }
}

对于 HTML 文件:

<ion-content padding> <ion-item *ngFor="let dat of datosarr"> <p>{{datosarr}}</p> </ion-item> </ion-content>

尝试在你身上添加这个config.xml

<allow-navigation href="http://ionic.local/*" />

编辑: 尝试添加:

<allow-intent href="*"/>
<allow-navigation href="http://*/*" />

记得删除平台再添加。

我运行遇到同样的问题

通过更改 http

解决

angular/http 仅适用于浏览器

import { Http } from '@angular/http';

为应用使用 ionic-native

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

更多详情可以阅读本页

swich httpclient for ionic