我正在尝试使用 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
上的选项
通过删除 unsafe-eval
根据您的配置,您已经准备就绪。
这对于开发来说是可以的,但对于生产环境来说就不行了。 Internet 上有一些很棒的实用程序可以帮助您为特定文件生成有效的 CSP。例如,Google 的“CSP header 生成器”。
关于javascript - 在主机定义的策略 : inline script. 中获取错误资源违反指令 'script-src ms-appx: ' unsafe-eval'' 资源将被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40063020/