简体   繁体   English

Ionic 5 - 无法显示来自 API 的数据

[英]Ionic 5 - Can't display data from API

Note : Total Ionic newbie here.注意:这里的总离子新手。

I have the following:我有以下内容:

  • Ionic 5 (Capacitor) app with Angular 11.带有 Angular 11 的 Ionic 5(电容器)应用程序。
  • Express backend (localhost:3000) Express 后端 (localhost:3000)

I can fetch data from an API call and display in the browser, but not on the emulated Android device.我可以从 API 调用中获取数据并在浏览器中显示,但不能在模拟的 Android 设备上显示。 I don't know how to check for console errors in Android Studio.我不知道如何在 Android Studio 中检查控制台错误。

This image can explain the situation better.这张图片可以更好地说明情况。

在此处输入图像描述

I think this is due to CORS.我认为这是由于 CORS。 I tried to follow the Ionic page on this but no resolution.我尝试关注Ionic 页面,但没有解决方案。

Here is my Express code:这是我的快递代码:

const express = require("express");
const cors = require("cors");
const app = express();
const port = 3000;

const allowedOrigins = [
  "capacitor://localhost",
  "ionic://localhost",
  "http://localhost",
  "http://localhost:8080",
  "http://localhost:8100",
  "http://192.168.2.25:8100",
];

// For parsing JSON in request body
app.use(express.json());

// MySQL connection details - for POC sake.
// In PROD, these are typically saved in .env variables
// Ref: https://www.linkedin.com/pulse/storing-database-credentials-securely-siddhesh-jog
var mysql = require("mysql");
var connection = mysql.createConnection({
  host: "____________________________.us-east-2.rds.amazonaws.com",
  user: "admin",
  password: "*****************",
  database: "poc",
});

const corsOptions = {
  origin: (origin, callback) => {
    if (allowedOrigins.includes(origin) || !origin) {
      callback(null, true);
    } else {
      console.log(origin);
      callback(new Error("Origin not allowed by CORS"));
    }
  },
};

// Enable preflight requests for all routes
app.options("*", cors(corsOptions));

// Connect to MySQL
connection.connect(function (err) {
  if (err) throw err;
  console.log("Connected!");
});

// Dashboard - GET
app.get("/dashboard", cors(corsOptions), (req, res) => {
  rows = [];
  connection.query(
    "select label_id, value from poc_fct",
    function (err, result) {
      if (err) throw err;
      res.json(result);
    }
  );
});

app.listen(port, () => {
  console.log(`CORS-enabled web server listening at http://localhost:${port}`);
});

Any help will be greatly appreciated.任何帮助将不胜感激。

What finally worked for me was changing the API endpoint from http://localhost:3000/data to http://192.168.2.25:3000/data , where 192.168.2.25 is the local IP address of the host where the Express server is running. What finally worked for me was changing the API endpoint from http://localhost:3000/data to http://192.168.2.25:3000/data , where 192.168.2.25 is the local IP address of the host where the Express server is跑步。

Few notes for anyone else who might have this issue in the future:对于将来可能遇到此问题的其他人,请注意以下几点:

  1. This isn't a CORS issue.这不是 CORS 问题。 I commented out app.use(cors)我注释掉了 app.use(cors)
  2. This isn't a HTTP/HTTPS issue这不是 HTTP/HTTPS 问题
  3. Changing the emulator's proxy to 10.0.2.2:3000 did not work将模拟器的代理更改为 10.0.2.2:3000不起作用

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

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