首页 文章

Cordova JqueryMobile:Ajax失败了

提问于
浏览
16

(最近6个小时一直在这里)我正在尝试制作一个phonegap / Cordova应用程序 . 我无法通过Android模拟器进行Ajax调用(API ver 22,Android> 4.4) . Ajax调用适用于Firefox桌面,但即使在Chrome浏览器上也会失败(与模拟器上的情况相同)

cordova --version 5.0.0

码:

$.ajax({
    url: serverUrl,
    type: 'GET',
    contentType: "application/json",
    async: true,
    dataType: 'jsonp',
    callback: 'callback',
    jsonpCallback: 'yourcallback',
    crossDomain: true,
    success: function (result) {
            $("#message").html("location sent");
        },
        error: function (request, error) {
            alert('Error ' + error);
        }
    });

我看到的错误是:

在chrome远程调试器上:

拒绝连接到'http://10.0.2.2.2/test/getLocation.php',因为它违反了以下内容安全策略指令:“default-src'self'数据:gap:https://ssl.gstatic.com '不安全-EVAL'” . 请注意,'connect-src'未显式设置,因此'default-src'用作后备 .

我在博客和帖子上看到了各种设置,但没有用 . 把一些在这里删除通常的嫌疑人 .

$.support.cors = true;
$.mobile.allowCrossDomainPages = true;

AppManifest可以访问Internet:

<uses-permission android:name="android.permission.INTERNET" />

config.xml文件:

<access origin="*" /> (have tried all variation, with putting actual server name here like "http://10.0.2.2" ).

真的需要你的帮助 . 累了又疼了:(

4 回答

  • 0

    我的错...

    我正在使用Phonegap示例html template..which有以下元标记阻止XSS .

    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    

    我不确定将这些东西放在一个示例代码中,是对还是不对..因为我浪费了我的2天 .

  • 10

    出于安全原因,您应该保留内容安全策略:

    Same-origin policy 是一个关键的安全机制 . 这限制了来自原点A的文档或脚本如何与来自源B的资源进行交互 . 这意味着URL http://store.comany.com/dir/page.html可以访问以下URL:

    但不是以下内容:

    (更多关于:https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

    However, attackers can bypass this policy with Cross-site scripting (XSS)

    要防止XSS和数据注入攻击,您可以使用 Content Security Policy (来自Here):

    内容安全策略(CSP)是一个增加的安全层,可帮助检测和缓解某些类型的攻击,包括跨站点脚本(XSS)和数据注入攻击 . 这些攻击用于从数据窃取到站点破坏或恶意软件分发的所有内容 . CSP旨在完全向后兼容;不支持它的浏览器仍然可以与实现它的服务器一起使用,反之亦然 . 不支持CSP的浏览器只是忽略它,像往常一样运行,默认为Web内容的标准同源策略 . 如果站点未提供CSP标头,则浏览器同样使用标准的同源策略 .


    tl;博士

    实际上,这已经在示例代码中了 . 但也许赞扬会很好=) . 您确实应该保留此配置以提高安全性 .

    在您的情况下,您必须将配置更改为:

    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src 'self' http://10.0.2.2">
    

    connect-src 限制您可以连接的原点(通过XHR,WebSockets和EventSource) . 你必须把'self'(对于设备上的脚本)和远程URL(例如http://10.0.2.2)放在这里

    • @Harry Martel提供了一个很好的链接,其中包含有关如何配置内容安全策略的示例 .

    • Here也是一篇概述配置属性的文章 .

  • 3

    您可以查看以下内容:

    https://github.com/apache/cordova-plugin-whitelist#content-security-policy

    内容安全策略有许多配置 .

  • 25

    错误信息:

    拒绝连接'http://some-address ' because it violates the following Content Security Policy directive: "default-src ' self'数据:gap:https://ssl.gstatic.com 'unsafe-eval'“ . 注意'connect-src'未明确设置,因此'default-src'用作后备 .

    Cordova 4/5/6“cordova create”命令

    cordova create yourproject com.yoursite.yourproject yourproject
    

    是使用此元标记生成项目

    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    

    检查index.html文件

    vi YourProject/plattforms/ios/www/index.html
    

    要么

    vi YourProject/plattforms/android/www/index.html
    

    您可以对该行进行评论,但请记住,这个政策可以满足您自己的应用需求,实际上有一个链接可以看到更多指导:

    README: content security policy

    一些说明:

    * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
    

相关问题