Cordova错误:拒绝执行内联脚本,因为它违反了以下内容安全策略指令

Per*_*ith 4 html javascript jquery node.js cordova

我正在学习使用Cordova和jquery mobile,我有以下错误:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:"default-src'self'data:gap:https://ssl.gstatic.com'unsafe-eval'".可以使用'unsafe-inline'关键字,散列('sha256-iacGaS9lJJpFDLww4DKQsrDPQ2lxppM2d2GGnzCeKkU =')或nonce('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>
Run Code Online (Sandbox Code Playgroud)

在课堂上,我写道:

cordova create hello2 com.example.hello2 hello2
cordova platform add android
cordova build 
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*den 13

您需要在Content-Security-Policy中添加"unsafe-inline"; 无论是使用default-src还是显式使用Javascript script-src.所以尝试类似的东西:

<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:;">
Run Code Online (Sandbox Code Playgroud)


Kri*_*ngh 5

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

<meta http-equiv="Content-Security-Policy" script-src='unsafe-inline';>
Run Code Online (Sandbox Code Playgroud)

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