【问题标题】:Cross side scripting with Cordova/ionic application使用 Cordova/ionic 应用程序编写横向脚本
【发布时间】:2015-06-11 01:09:23
【问题描述】:

Cordova 不允许对另一个 URL 的获取请求。

互联网指向这个插件https://github.com/agamemnus/cordova-plugin-whitelist

但是,关于如何安装它的信息为零?这是我的第一个 ionic cordova 应用程序。

问题 如何允许跨端脚本,以便我可以使用此插件从 cordova 应用程序中点击填充文本?

JS:

        var results = document.getElementById('results');
        var r = new XMLHttpRequest();
        r.open("GET", "http://www.filltext.com?rows=10&f={firstName}", true);
        r.onreadystatechange = function () {
          if (r.readyState != 4 || r.status != 200) return;
          var data = JSON.parse(r.responseText);
          console.log(data);
          for (var i=0;i<data.length;i++){
                results.innerHTML += '<li>'+data[i].f+'</li>';
          }
        };
        r.send();

更新

未捕获的 EvalError:拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下内容安全策略指令中允许的脚本来源:“default-src 'self'”。 Config.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.megster.nfc.reader.ionic" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>NFC Reader</name>
  <description>
        PhoneGap NFC Reader rewritten with Ionic Framework
    </description>
  <author email="don.coleman@gmail.com" href="https://github.com/don">
      Don Coleman
    </author>
  <content src="index.html"/>
  <access origin="*"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="UIWebViewBounce" value="false"/>
  <preference name="DisallowOverscroll" value="true"/>
  <preference name="BackupWebStorage" value="none"/>
  <feature name="StatusBar">
    <param name="ios-package" value="CDVStatusBar" onload="true"/>
  </feature>
</widget>

index.html

 <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <script src="js/filters.js"></script>
    <script src="lib/ngCordova-nfc/nfc.js"></script>
    <script src="js/app.js"></script>

【问题讨论】:

    标签: javascript cordova ionic


    【解决方案1】:

    要安装cordova插件,这里是标准合成器

    cordova plugin add cordova-plugin-whitelist
    

    或者你可以直接指向 git:

    cordova plugin add https://github.com/agamemnus/cordova-plugin-whitelist
    

    那么如果你的config.xml已经有

    如果您的 config.xml 有访问源行:&lt;access origin="*"/&gt;

    最简单(但最不安全)的方法是将此行添加到您的 index.html 标头中。

    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; frame-src 'self' https://cordova.apache.org">
    

    在插件页面https://github.com/agamemnus/cordova-plugin-whitelist查看更多信息

    【讨论】:

    • 更新了我收到错误的问题:拒绝将字符串评估为 JavaScript,因为在以下内容安全策略指令中,'unsafe-eval' 不是允许的脚本来源:“default-src 'self' ”。请注意,'script-src' 没有显式设置,因此 'default-src' 用作后备。
    • Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not a allowed source of script in the following Content Security Policy directive:"default-src 'self'".跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多