Fetch API 接口允许 Web 浏览器向 Web 服务器发出 HTTP 请求。
不再需要 XMLHttpRequest。
浏览器支持
表中的数字注明了完全支持 Fetch API 的首个浏览器版本:
Fetch API 实例
下面的例子获取文件并显示内容:
实例
fetch(file)
.then(x => x.text())
.then(y => myDisplay(y));
由于 Fetch 基于 async 和 await,因此上面的例子这么写可能更容易理解:
实例
async function getText(file) {
let x = await fetch(file);
let y = await x.text();
myDisplay(y);
}
甚至可以更好:请使用易于理解的名称而不是 x 和 y:
实例
async function getText(file) {
let myObject = await fetch(file);
let myText = await myObject.text();
myDisplay(myText);
}
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64493.shtml