前言
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set.
Fetch API 提供了一个获取资源的接口(包括跨域)。任何使用过 XMLHttpRequest
的人都能轻松上手,但新的API提供了更强大和灵活的功能集。
这是官方API中的第一句话,可以看出fetch
是ES6中新增的,用于取代XMLHttpRequest
的网络请求框架,它比之更强大。下面我们来下它的使用。
Fetch
fetch返回的其实是一个Promise
函数。我们先来看一个完整的请求代码:
const url = '192.168.32.45:8081/login.shtml'
fetch(url, {
method: 'POST',
headers: {