javascript - firefox OS 内容安全策略错误 Index.html 中基于 XHR 的应用程序?

标签 javascript ajax firefox-os

我在控制台中遇到了这个错误。

内容安全策略:该页面的设置阻止了自行加载资源(“script-src app://fa91d835-176d-4fe7-bd06-fe7f57f11b68”)。

我尝试创建一个 firefox ajax 应用程序来从我的 codeigniter Controller 中检索一些数据。 当我检查控制台时,它只返回错误。我只在文件外添加了 javascript 函数。但它说 CSP 错误。

我的索引.Html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Privileged app</title>
        <meta name="description" content="A privileged app stub">
 <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" href="css/app.css">
<script type="text/javascript" src="js/app.js" ></script>

        <script type="text/javascript" src="js/jquery-1.7.1.min.js" ></script>
        <script type="text/javascript" src="js/xhrapp.js" ></script>
   <link rel="prefetch" type="application/l10n" href="data/locales.ini" />
        <script type="text/javascript" src="js/libs/l10n.js" ></script>
    </head>
    <body>
        <section>
            <h1 data-l10n-id="app_title">Privileged empty app</h1>
            <p data-l10n-id="app_description">This app is empty. Fill it with your own stuff!</p>
            <p id="message"></p>
            <input type="text" id="ajax_data" value="">
            <a href="" onclick="xhrapp();"><button>Click</button></a>
        </section>
    </body>
</html>

函数xhrapp.js

function xhrapp(){
    var a=$("#ajax_data").val();
    alert(a);
    console.log("in function");
    var xhr = new XMLHttpRequest({
            mozSystem: true
    });
    // xhr.open("POST", "http://blac.byethost7.com/home/index.php/welcome/demo");
    xhr.open("POST", "http://localhost/shop/home/home/demo");
    xhr.send(a);
    xhr.onload = function() {
    if (xhr.status == 200) {
        console.log(xhr.responseText);
        // alert(xhr.responseText);
      }
    };


}

请帮帮我!

最佳答案

你不能使用属性 onclick='' 因为它违反了 CSP

由于您使用的是 jquery,因此请将其添加到您的 xhrapp.js 中

$(document).ready(function(){
   $("#mybutton").on("click", xhrapp);

});

将您的 HTML 更改为:

<button id="mybutton">Click</button> <!-- <a> is not needed -->

更多信息请查看: https://developer.mozilla.org/en-US/Apps/Build/Building_apps_for_Firefox_OS/CSP

关于javascript - firefox OS 内容安全策略错误 Index.html 中基于 XHR 的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32836348/

相关文章:

c# - 文本框控件中的验证问题

javascript - 如何通过 AJAX 发送 PHP 数组?

ajax - 更新从另一个 Controller 呈现的 View 内的模板

javascript - 在 Firefox OS 验证器中检测到 CSP 违规

javascript - KML 文件加载但不显示 - 为什么不呢?

javascript - hashbang vs hijax

JavaScript 和 EM

javascript - 在另一个 .js 文件中调用 js 函数

javascript - 由用户动态生成新页面(Firefox OS)

javascript - Firefox OS - BuildingBlock 抽屉的使用