![](/img/trans.png)
[英]Ionic React / Capacitor, requests work on web version and with postman but not on a device
[英]Ionic/Capacitor React App API Requests returning HTML on iOS
所有 API 對 localhost 服務器的請求都返回 index.html 文件 iOS 構建上的 iOS 構建的 React.js 應用程序,但請求在瀏覽器和 PWA 構建上工作正常。
請求返回預期的數據(通常是 JSON)。
通常,API 向 http://localhost:3000/api/[route] 請求 go。 在 iOS 構建中,他們將訪問電容器://localhost/api/[route]
因為該路線返回 HTML 而不是 JSON 數據,所以我收到以下錯誤(其中一個錯誤,因為每個 API 路線都有相同的錯誤)導致白屏:
TypeError: undefined is not a function (near '...a.map...')
我嘗試將主機名(生產服務器所在的位置)添加到我的根目錄中的電容器.config.json 文件中,但它仍然失敗。 開發 API 服務器在 localhost:3000 上運行。
嘗試在配置中將服務器主機名設置為“localhost:3000”。
"server": {
"hostname": "localhost:3000"
},
正如電容器配置文檔中提到的那樣,“http”不允許作為 iosScheme:不能設置為 WKWebView 已經處理的方案,例如 http 或 https]( https://developer.apple.com/documentation /webkit/wkwebviewconfiguration/2875766-seturlschemehandler )
因為該站點適用於除此 iOS 構建之外的所有其他構建,所以問題應該在這里......
如何將我在 iOS 構建上的請求路由到正確的位置?
If I set the server.url option in the capacitor.config.json file to https://www.website.com , I'm able to pull data from my production server, but when the authorization check returns a 401, the app似乎掛在啟動屏幕上,我在控制台中收到的唯一類似錯誤的消息是響應返回了 401(如預期的那樣)
根據您提供的少量信息,我將嘗試猜測出了什么問題。
/api/[route]
進行 XHR/fetch 調用,瀏覽器將 append 向其發送方案和主機名,並將其轉換為capacitor://localhost:3000/api/[route]
。 那 url 屬於應用程序,而不是 API 正在運行的 web 服務器。看起來你在第 1 點,一旦你修復它,你會達到第 2 點,然后可能會達到第 3 點。
第一個問題是通過將電容器.config.json文件中的server.url參數設置為生產服務器的URL來解決的:
"server":{
"url":"https://www.website.com"
}
第二個問題是確保來自@capacitor/splash-screen 的啟動屏幕隱藏代碼實際上已部署到 index.js 文件中的 prod 上。
import { SplashScreen } from '@capacitor/splash-screen';
setTimeout(() => {
SplashScreen.hide();
}, 2000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.