【问题标题】:firefox OS Content Security Policy error XHR based application in Index.html?Firefox OS 内容安全策略错误 Index.html 中基于 XHR 的应用程序?
【发布时间】:2015-09-29 05:33:46
【问题描述】:

我在控制台中收到此错误。

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

我尝试创建一个 firefox ajax 应用程序来从我的 codeigniter 控制器中检索一些数据。 当我检查控制台时,它只返回错误。我只在文件之外添加了 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);
      }
    };


}

请帮帮我!

【问题讨论】:

  • 那个控制器有。回声“嗨”;仅限。

标签: javascript ajax firefox-os


【解决方案1】:

您不能使用属性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

【讨论】:

  • 它已修复。但是我遇到了另一个问题:(跨域请求被阻止:同源策略不允许读取blac.byethost7.com/home/index.php/welcome/demo 的远程资源。这可以通过将资源移动到同一个域或启用 CORS 来解决。
  • 如果您管理该资源,我建议将其移至同一域以避免头痛!
  • 如何将其移至同一个域?我正在本地系统上的 Firefox OS 模拟器中运行该应用程序。我也在本地主机中尝试过。同样的错误返回:(
  • 在本地文件夹(app 文件夹)中... { "version": "0.1.0", "name": "xhrapplication", "description": "最小结构的模板", "launch_path": "/index.html", "icons": { "16": "/img/icons/icon16x16.png", "48": "/img/icons/icon48x48.png", "60 ": "/img/icons/icon60x60.png", "128": "/img/icons/icon128x128.png" },
  • 从来没有遇到过这个问题,但我相信它与你的清单有关:developer.mozilla.org/en-US/Apps/Build/Manifest
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 2020-05-28
  • 2022-01-08
  • 1970-01-01
  • 2019-02-11
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多