fetch and object

使用fetch:

因为在 Request 和 Response 中都包含 Body 的实现,所以包含以下类型:

    ArrayBuffer
    ArrayBufferView (Uint8Array and friends)
    Blob/File
    string
    URLSearchParams
    FormData

在 fetch 中实现了对应的方法,并返回的都是 Promise 类型。那如何获取到我们的json数据呢?

这样处理返回的数据类型就会变的特别地方便,如处理 json 格式的数据:

    var myRequest = new Request('http://api.com/products.json');
     
    fetch(myRequest).then(function(response) {
      return response.json().then(function(json) {
        for(i = 0; i < json.products.length; i++) {
          var name = json.products[i].Name;
          var price = json.products[i].Price;
          // do something more...
        }
      });
    });

使用object:

猜你喜欢

转载自www.cnblogs.com/925039847z/p/10049975.html