Hybrid app : load file local

Không thể dùng fetch để load local được, vì vậy phải build 1 function mới

function fetchLocal(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest
    xhr.onload = function() {
      resolve(new Response(xhr.responseText, {status: xhr.status}))
    }
    xhr.onerror = function() {
      reject(new TypeError('Local request failed'))
    }
    xhr.open('GET', url)
    xhr.send(null)
  })
}

Tham khảo
https://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript
https://jakearchibald.com/2015/thats-so-fetch/

Cách 2: Chưa thử

var path = window.location.href.replace(‘index.html’, );
$.getJSON(path + “fixtures/initial.json”, function(data)
{
    // do something with data
});
http://www.erol.si/2013/10/how-to-read-local-json-file-with-phonegap-jquery-mobile/
Advertisements