无法加载http javascript源代码

无法加载http javascript源代码,javascript,android,http,https,capacitor,Javascript,Android,Http,Https,Capacitor,我正在尝试从本地web服务器的url下加载远程脚本/css 但电容器android似乎有错误,电容器ios运行良好。 我还设法从中加载css,没有任何错误 我猜是不是不支持http 这是我加载它的代码 <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootst

我正在尝试从本地web服务器的url下加载远程脚本/css

但电容器android似乎有错误,电容器ios运行良好。 我还设法从中加载css,没有任何错误

我猜是不是不支持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>
My capactor.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": {}
}
得到答案

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

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

另外,请注意,使用127.0.0.1在实际设备上不起作用,因为 同样的原因,最好使用计算机的本地IP 相反,类似于192.168.1.30

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