繁体   English   中英

电容 Android 未加载 http javascript 源

[英]Capacitor Android not loading http javascript source

Im trying to load remote scripts / css from my local web server under url of http://127.0.0.1:4002/

但似乎电容器 android 有错误,电容器 ios 工作正常。 我还设法从没有任何错误https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.ZC7A628CBA22E2A28EB17B5F5F5C

我的猜测是不支持 http?

这是我加载它的代码

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.css" />
        <link href="http://127.0.0.1:4002/test/fonts.css" rel="stylesheet" />
        <meta charset="utf-8" />
        <meta
            name="viewport"
            content="initial-scale=1, maximum-scale=1.0, minimum-scale=1.0, width=device-width, height=device-height, viewport-fit=cover,  user-scalable=no"
        />
        <title>My Title</title>
    </head>

    <body id="body">
        
    </body>
</html>

这是我在“chrome://inspect/#devices”下遇到的错误在此处输入图像描述

我的依赖:

"@capacitor-community/http": "^1.0.0-alpha.1",
"@capacitor/android": "^3.0.0-rc.0",
"@capacitor/cli": "^3.0.0-rc.0",
"@capacitor/core": "^3.0.0-rc.0",
"@capacitor/device": "^0.5.5",
"@capacitor/dialog": "^0.4.5",
"@capacitor/filesystem": "^0.5.1",
"@capacitor/ios": "^3.0.0-rc.0",

我的电容器.config.json

{
    "appId": "com.example.app",
    "appName": "hello-cap-2",
    "bundledWebRuntime": false,
    "npmClient": "npm",
    "webDir": "www",
    "plugins": {
        "SplashScreen": {
            "launchShowDuration": 0
        }
    },
    "server": {
        "allowNavigation": ["*"]
    },
    "android": {
        "allowMixedContent": true
    },
    "cordova": {}
}

https://github.com/ionic-team/capacitor/discussions/4477得到了答案

127.0.0.1是你的电脑,iOS模拟器理解它,因为模拟器在你的电脑上运行,但是在Android上,模拟器是一个“虚拟机”,有自己的网络,所以127.0.0.1是你自己的模拟器,计算机。

您可以启用端口转发以将 4002 端口流量发送到模拟器,或者您可以使用 10.0.2.2 IP,因为它会自动循环到 127.0.0.1

https://developer.android.com/studio/run/emulator-networking

另外,请注意,由于同样的原因,使用 127.0.0.1 在真实设备上不起作用,最好使用计算机的本地 IP 代替,例如 192.168.1.30

暂无
暂无

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

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