fetch请求file://格式json文件

(function() {
  var nativeFetch = window.fetch;
  if(nativeFetch) {
    var a = document.createElement('a');
    window.fetch = function(url) {
      a.href = url;
      var full_url = a.href;
      if(full_url.indexOf('file:') === 0) {
        return new Promise(function(resolve, reject) {
          var xhr = new XMLHttpRequest();
          xhr.onload = function() {
            resolve(new Response(xhr.responseText, {status: xhr.status}));
          };
          xhr.onerror = function(err) {
            reject(new TypeError('Local request failed'));
          };
          xhr.open('GET', url);
          xhr.send(null);
        });
      }
      else {
        return nativeFetch.call(this, Array.prototype.slice.apply(arguments));
      }
    };
  }
})();

标签: JavaScript

This page loaded in 0.002759 seconds