简体   繁体   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/ Im trying to load remote scripts / css from my local web server under url of http://127.0.0.1:4002/

But seem to be having error in capacitor android, capacitor ios is working fine.但似乎电容器 android 有错误,电容器 ios 工作正常。 I also manage to load the css from without any error https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.css我还设法从没有任何错误https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap-grid.min.ZC7A628CBA22E2A28EB17B5F5F5C

My guess is not supporting http?我的猜测是不支持 http?

Here is my code in loading it这是我加载它的代码

<!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>

Here is the error i got under 'chrome://inspect/#devices'这是我在“chrome://inspect/#devices”下遇到的错误在此处输入图像描述

My dependencies:我的依赖:

"@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",

My capacitor.config.json我的电容器.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": {}
}

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

127.0.0.1 is your computer, iOS simulator understand it because the simulator is running on your computer, but on Android, the emulator is a "virtual machine" with its own networking, so 127.0.0.1 for it its the own emulator, not your computer. 127.0.0.1是你的电脑,iOS模拟器理解它,因为模拟器在你的电脑上运行,但是在Android上,模拟器是一个“虚拟机”,有自己的网络,所以127.0.0.1是你自己的模拟器,计算机。

You can enable port forwarding to send the 4002 port traffic to the emulator, or you can use 10.0.2.2 IP as it automatically loops to 127.0.0.1您可以启用端口转发以将 4002 端口流量发送到模拟器,或者您可以使用 10.0.2.2 IP,因为它会自动循环到 127.0.0.1

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

Also, note that using 127.0.0.1 won't work on real devices because of the same reason, it's better if you use the local IP of the computer instead, something like 192.168.1.30另外,请注意,由于同样的原因,使用 127.0.0.1 在真实设备上不起作用,最好使用计算机的本地 IP 代替,例如 192.168.1.30

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

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