我正在為我的業務開發一個新應用程式,我需要將我的 php 網路應用程式轉換為移動應用程式。
我在嘗試從我的 php 檔案中獲取 json 資料時遇到問題,該檔案是:
<?php
header("Access-Control-Allow-Credentials: true");
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
$api = true; // this is to bypass redirection to login screen
require(__DIR__ . '/../autoload.php');
if (isset($_GET['TOKEN']) && $_GET['TOKEN'] = '???') {
.... // Some code to get my values
$res = array();
.... // more code to get the values
echo json_encode($res);
}else{
die('Invalid token');
}
我一直在使用https://jsonplaceholder.typicode.com/todos/1在我的 ts 檔案中測驗 httpclient ,它正在作業。這是我的 ts 檔案:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
orders: any[] = [];
constructor(private http: HttpClient) {}
ngOnInit() {
this.getOrders().subscribe((value) => {
for (let key in value) {
console.log(value[key]);
console.log(key);
this.orders.push({
shop: key,
orders: value[key],
});
}
});
}
getOrders() {
console.log('function run 2');
return this.http.get(
'https://localhost/???/api/get???.php?TOKEN=???',
{}
);
}
}
在使用它的瀏覽器上ionic serve,我的 localhost api 就像一個魅力,但是當我運行ionic cordova run android --prod -l --consolelogs它時,它只使用 jsonplaceholder 測驗 api 而不是我的 localhost api
您是否認為問題出在我的 php 檔案的 headers 設定、我的 get 請求甚至我的 wamp localhost 存盤 php 檔案的位置?
編輯
My ssl certificate seems to not be valid, but I’ve tested the same api of jsonplaceholder and it was working with http, I’ve already tried to put my php file on a server that is running with a valid ssl certificate and it wasn’t working too, that’s why I’m quiet lost
uj5u.com熱心網友回復:
經過長時間的研究,我找到了解決此問題的方法(至少繞過):
我剛剛將我的運行或服務函式與 --external 一起使用,并使用用于服務我的專案的公共 ip 呼叫了我的 php 檔案(與存盤我的 php 檔案的本地主機 ip 相同)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/440566.html
標籤:php angular api cordova ionic-framework
