javascript - 在主机定义的策略 : inline script. 中获取错误资源违反指令 'script-src ms-appx: ' unsafe-eval'' 资源将被阻止

标签 javascript html visual-studio

我正在尝试使用 Visual Studio 制作一个通用的 Windows 应用程序。当我尝试运行以下代码时:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>testProject</title>
    <link href="css/default.css" rel="stylesheet" />
</head>
<body>
    <div>Content goes here!</div><script src="js/main.js"></script>
    <form name="myForm">

           <input type="radio" name="myRadio" value="yes"/>  Yes <br />
           <input type="radio" name="myRadio" value="No"/> No <br />
           <input type="button" name="submit"value="submit" onsubmit="return validateForm" />
    </form>
</body>
</html>

在 JavaScript 主文件中:

function ValiditeForm(){

    document.writeln("Hello World");
}

当我尝试运行时,我收到以下警告:

CSP14312:主机定义策略中的资源违反指令“script-src ms-appx:‘unsafe-eval’”:内联脚本。资源将被阻止。

我只是想尝试使用 visual studio 通用应用程序编写 HTML 代码,当用户单击 HTML 表单中的单选按钮时,它将被传递给 JavaScript 中的函数并打印出一个值。

最佳答案

这是因为内容安全策略而发生的

来自 MDN :

The HTTP Content-Security-Policy response header allows web site administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This helps guard against cross-site scripting attacks (XSS).

您的服务器正在发送 Content-Security-Policy HTTP header 阻止您的浏览器信任您的 javascript 文件。

您可以更改 CSP header ,以便内联 <script> ,如果您的服务器是 IIS,请查找 HTTP Response heathers inetmgr.exe 上的选项

inetmgr

通过删除 unsafe-eval根据您的配置,您已经准备就绪。

unsafe-eval

这对于开发来说是可以的,但对于生产环境来说就不行了。 Internet 上有一些很棒的实用程序可以帮助您为特定文件生成有效的 CSP。例如,Google 的“CSP header 生成器”。

关于javascript - 在主机定义的策略 : inline script. 中获取错误资源违反指令 'script-src ms-appx: ' unsafe-eval'' 资源将被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40063020/

相关文章:

javascript - 过滤此正则表达式中的字符

c++ - 在 Visual Studio Debug模式下编译的可执行文件 : jmp to function body instead of direct address in call

Git 无法忽略 Visual Studio 中的 node_modules

javascript - 从模态调用 AngularJS Promise 会导致错误

javascript - 如何通过javascript单击按钮将焦点设置在div上

html - 内联 css 背景在 Chrome/Safari/IE 上不显示,但在 Mozilla Firefox 上有效

javascript - 在其他页面中使用更改的值

c++ - 包含库是行不通的

javascript - jQuery AJAX 调用 - event.preventDefault();不停止任何浏览器中的 PHP 表单提交

javascript - 如何在 html 中创建数据模板