【问题标题】:Cordova app fails to load local CSS/JS, but remote is fineCordova 应用程序无法加载本地 CSS/JS,但远程很好
【发布时间】:2016-10-04 06:53:28
【问题描述】:

我有一个 Cordova 应用程序,在从远程服务器下载 CSS/JS 时运行良好,但在从本地 css/js 文件夹加载文件时失败。

这是我的index.html

<!doctype html>
<html ng-app="myapp.editor" ng-class="'appmode-' + appMode" check-if-touch-device>

<head>
    <base href="/">
    <meta charset="utf-8">
    <!-- <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>My App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <!-- Here are the CSS/JS files  -->
    <link rel="stylesheet" type="text/css" href="css/myapp.css">
    <script src="js/myapp.js"></script>

</head>

    <body ng-strict-di>

        <script type="application/json" id="ddd">
            console.log('Testing...');
        </script>

        <div class="container animate-view {{screenTransition}}" ui-view></div>

        <script type="text/javascript" src="cordova.js"></script>

    </body>

</html>

这里是 config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.weld.myapp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>My App</name>
    <description>,,,</description>
    <author email="contact@myapp.test" href="https://www.myapp.test">Weld</author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <access origin="https://d12wzx9jhww7k1.cloudfront.net" />
    <access origin="https://d3kzdohl1nxwtg.cloudfront.net" />
    <access origin="https://ds9izj7ug4pro.cloudfront.net" />
    <access origin="https://www.myapp.test" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
        <splash src="res/splash_512.png" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <splash src="res/splash_512.png" />
    </platform>
    <platform name="browser">
        <splash src="res/splash_512.png" />
    </platform>
    <plugin name="cordova-plugin-splashscreen" spec="~4.0.0" />
    <icon src="res/myapp_icon.png" />
    <engine name="ios" spec="~4.2.1" />
    <engine name="android" spec="~5.2.2" />
</widget>

我认为它可能与 Content-Security-Policy 相关,但当我启用它时,我只是得到一个白屏。

【问题讨论】:

  • 您尝试在哪个平台/操作系统版本上运行您的代码?您使用哪个cordova-android 或cordova-ios 版本?我的所有 css 和 js 文件都本地存储在我的 cordova 应用程序中,并且到目前为止从未遇到过问题(我使用带有 cordova-android 3.6.4 和 cordova-ios 3.6.3 的 cordova CLI 4.0)。
  • 文件是否存储在应有的位置(www/index.htmlwww/css/myapp.csswww/js/myapp.js)?

标签: cordova


【解决方案1】:

您正在使用 HTML 的base tag,它指的是相对于所有资源的 URL,如果您删除该行,您可以在执行操作时访问 js/css 文件。否则,您必须输入文件的完整路径。

【讨论】:

  • 谢谢!本地 Cordova 捆绑包中的完整路径是什么?或者你的意思是我硬盘上的完整路径?
  • 我认为这是与应用程序目录相关的路径,所以如果你输入 "" 可能会起作用,但我不确定。试试看告诉我!
猜你喜欢
  • 1970-01-01
  • 2014-07-02
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2012-09-25
  • 1970-01-01
相关资源
最近更新 更多