天天看點

ionic報各種詭異錯誤的解決辦法

  • -

1.img标簽上添加onerror事件時,報錯:

Refused to execute inline event handler because it violates the following Content Security Policy directive: “default-src ‘self’ data: gap: https://ssl.gstatic.com ‘unsafe-eval’”. Note that ‘script-src’ was not explicitly set, so ‘default-src’ is used as a fallback.

“, source: file:///android_asset/www/index.html (41)

解決辦法:

将index.html頂端的

注釋掉

2.開啟應用的時候報錯Application error : the connection to the server was unsuccessful

這個錯誤一般出現在啟動頁面加載資料時間過長

是以首先你需要在config.xml修改加載逾時時長:

<!--預設值為10000 機關毫秒-->
<preference name="LoadUrlTimeoutValue" value="50000"></preference>
           

然後盡量減少引用網絡js,能下載下傳為本地的都下載下傳下來放到項目裡

不能下載下傳的放到使用頁面加載:

//以加載百度地圖js為例
//聲明方法
loadjs = function (src, func) {
    //判斷這個js檔案存在直接執行回調
    var scripts = document.getElementsByTagName('script');
    for (i in scripts)
      if (scripts[i].src == src)
        return func();
    if (typeof func != 'function') {
      console.log('param 2 is not a function!!');
      return false;
    }
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = src;
    var head = document.getElementsByTagName('head').item();
    head.appendChild(script);

    script.onload = function () {
      func();
    }
  }

//調用
loadjs('http://api.map.baidu.com/getscript?v=2.0&ak=t8zuH8VGAlkVHtn28RY4ens1tszPhFEv&services=&t=20160804144823', function () {
       //這裡寫加載完成後執行的代碼
        }
      });
           

最後把首頁要從背景拉取資料的代碼都寫到deviceready事件中.代碼如下:

//這個事件觸發時機為應用與手機連接配接初始化完成後
document.addEventListener("deviceready", function () {
      //執行的代碼
    }, false);
           

後續陸續添加