草庐IT

javascript - Cordova 错误 : Refused to execute inline script because it violates the following Content Security Policy directive

coder 2025-01-20 原文

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

Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Either the 'unsafe-inline' keyword, a hash ('sha256-iacGaS9lJJpFDLww4DKQsrDPQ2lxppM2d2GGnzCeKkU='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

我的代码如下:

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

最佳答案

您需要在 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:;">

关于javascript - Cordova 错误 : Refused to execute inline script because it violates the following Content Security Policy directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33494516/

有关javascript - Cordova 错误 : Refused to execute inline script because it violates the following Content Security Policy directive的更多相关文章

随机推荐