admin管理员组

文章数量:1388885

Hi I'm building an iOS application via Cordova(5.1.1)/Phonegap and I have a problem I can´t solve.

A basic Ajax call throws a SecurityError: DOM Exception 18 I tried all the tricks regarding whitelisting and now I'm lost.. Anyone who can help? Thanks.

Here is what I do after device is ready:

    var getUrl = '/'+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( "plete" );
        });

getUrl is: .11032230061141x73.11032230061141x-20.572796183027627x42.36447502674581

And I get:

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] plete

Hi I'm building an iOS application via Cordova(5.1.1)/Phonegap and I have a problem I can´t solve.

A basic Ajax call throws a SecurityError: DOM Exception 18 I tried all the tricks regarding whitelisting and now I'm lost.. Anyone who can help? Thanks.

Here is what I do after device is ready:

    var getUrl = 'http://shopplo./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( "plete" );
        });

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

And I get:

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] plete
Share Improve this question edited Jul 19, 2015 at 23:44 cjhveal 5,8212 gold badges29 silver badges38 bronze badges asked Jul 19, 2015 at 23:20 Jonny HøjdamJonny Højdam 531 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Check your meta tag. By default, it uses:

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

use the code below to enable all requests

<!-- 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'">

The above answer errors due to the syntax being incorrect.

Below is correct:

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

Rather than allowing everything, you could just allow the urls that you are making the ajax call to. For example, if I wanted to get something from the facebook API, I could have something like:

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

本文标签: javascriptAjax call on Cordova ios SecurityError DOM Exception 18Stack Overflow