2015-07-19 26 views
4

嗨我通過科爾多瓦(5.1.1)/ Phonegap構建iOS應用程序,我有一個問題,我無法解決。科爾多瓦Ajax調用ios SecurityError:DOM異常18

一個基本的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 

回答

2

檢查您的meta標籤。 默認情況下,它使用:

<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'"> 
+0

是的!那是做的..謝謝你克林特。 –

+0

這是一個主要的LIFESAVER。上週我花了2-3個小時的搜索,試用和錯誤,直到我發現了這個。我正在使用並在博客文章中引用此解決方案(以解決另一個問題):http://www.brodybits.com/cordova/sqlite/api/jquery/2015/10/26/populating-cordova-sqlite-storage -with-the-jquery-api.html – brodybits

+1

安全警告:正如[Cordova/Phonegap的白名單系統](https://groups.google.com/forum/#!topic/phonegap/jUw5hdyHoZw)中所述PhoneGap forum topic ,此解決方案關閉所有安全性,不建議使用。 CSP標籤已經在https://github.com/apache/cordova-plugin-whitelist上記錄了很長一段時間。最近有人發表了博文:https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/the-whitelist-system.md – brodybits

2

以上回答錯誤的所有請求。

下面是正確的:

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

而不是讓一切,你可以只允許你做的Ajax調用的URL。例如,如果我想從facebook API獲得某些東西,我可以使用類似於:

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