【问题标题】:Cordova error: Refused to execute inline script because it violates the following Content Security Policy directiveCordova 错误:拒绝执行内联脚本,因为它违反了以下内容安全策略指令
【发布时间】:2017-05-24 03:49:09
【问题描述】:

我正在学习将 Cordova 与 jquery mobile 一起使用,但出现以下错误:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“default-src 'self' data: gap: https://ssl.gstatic.com'unsafe-eval'”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-iacGaS9lJJpFDLww4DKQsrDPQ2lxppM2d2GGnzCeKkU=”)或随机数(“nonce-...”)。另请注意,'script-src' 未显式设置,因此 'default-src' 用作后备。

我的代码如下:

    <!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
        <script> 
            $(document).ready(function()
            { 
                $("#tryit").click(function() {   
                    document.getElementById("msg").innerHTML = "hello";
                });

            }); 
        </script>
    </head>
    <body> 
        <button id="tryit">Try it</button>
        <div id="msg"></div>

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

在课堂上,我是这样写的:

cordova create hello2 com.example.hello2 hello2
cordova platform add android
cordova build 

我认为这与 "cordova-plugin-whitelist" 有关 ,但我不知道如何卸载 NPM

【问题讨论】:

    标签: javascript jquery html node.js cordova


    【解决方案1】:

    只需在 index.html 中添加此代码

    <meta http-equiv="Content-Security-Policy" script-src='unsafe-inline';>
    

    欲了解更多信息,请访问: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#Unsafe_inline_script

    【讨论】:

      【解决方案2】:

      您需要在 Content-Security-Policy 中添加“unsafe-inline”;到default-src 或使用script-src 明确用于Javascript。所以试试类似的东西:

      <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-26
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 2021-04-22
        相关资源
        最近更新 更多