【问题标题】:Ajax call on Cordova ios SecurityError: DOM Exception 18Cordova ios SecurityError 上的 Ajax 调用:DOM 异常 18
【发布时间】:2015-10-09 00:18:09
【问题描述】:

您好,我正在通过 Cordova(5.1.1)/Phonegap 构建一个 iOS 应用程序,但有一个无法解决的问题。

基本的Ajax 调用会引发SecurityError: DOM Exception 18 我尝试了有关白名单的所有技巧,但现在我迷路了.. 谁能帮忙?谢谢。

这是我在设备准备好后要做的事情:

    var getUrl = 'http://shopplo.com/api/posts/radius/'+app.lat_min+'x'+app.lat_max+'x'+app.lng_min+'x'+app.lng_max+'';

    //console.log(getUrl);

    var getPosts = $.ajax({
          method: 'GET',
          url: getUrl,
          dataType: 'JSON'
        })
        .done(function(e) {
            console.log( e );


        })
        .fail(function(e) {
            //console.log( "error");

            $.each(e, function(key, element) {
                console.log('key: ' + key + '\n' + 'value: ' + element);
            });

        })
        .always(function() {
            console.log( "complete" );
        });

getUrl 是:http://shopplo.com/api/posts/radius/37.11032230061141x73.11032230061141x-20.572796183027627x42.36447502674581

我得到:

2015-07-20 01:12:55.981 ShopploLight[779:568632] key: responseJSON :: value: undefined
2015-07-20 01:12:55.983 ShopploLight[779:568632] key: status :: value: 0
2015-07-20 01:12:55.983 ShopploLight[779:568632] key: statusText :: value: Error: SecurityError: DOM Exception 18
2015-07-20 01:12:55.984 ShopploLight[779:568632] complete

【问题讨论】:

    标签: javascript ios ajax cordova


    【解决方案1】:

    您可以只允许您进行 ajax 调用的 url,而不是允许一切。例如,如果我想从 facebook API 获得一些东西,我可以有类似的东西:

    <meta http-equiv="Content-Security-Policy" content="style-src 'self' 'unsafe-inline'; script-src: 'self' https://graph.facebook.com">
    

    【讨论】:

      【解决方案2】:

      由于语法不正确导致上述答案错误。

      以下是正确的:

      <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval';">
      

      【讨论】:

        【解决方案3】:

        检查您的元标记。 默认情况下,它使用:

        <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 *">
        

        使用下面的代码启用所有请求

        <!-- Enable all requests, inline styles, and eval() -->
        <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src: 'self' 'unsafe-inline' 'unsafe-eval'">
        

        【讨论】:

        猜你喜欢
        • 2015-02-19
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多