text 非同步取得JSON(使用无极)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 非同步取得JSON(使用无极)相关的知识,希望对你有一定的参考价值。

const getJSON = function(url) {
  const promise = new Promise(function(resolve, reject) {
    const handler = function() {
      if (this.readyState !== 4) {
        return;
      }
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(new Error(this.statusText));
      }
    };
    const client = new XMLHttpRequest();
    client.open("GET", url);
    client.onreadystatechange = handler;
    client.responseType = "json";
    client.setRequestHandler("Accept", "application/json");
    client.send();
  });
  
  return promise;
};

getJSON("/posts.json").then(function(json) {
  console.log("Contents: " + json);
}, function(error) {
  console.error('Error: ', error);
});

以上是关于text 非同步取得JSON(使用无极)的主要内容,如果未能解决你的问题,请参考以下文章