<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>fetch</title>
</head>
<body>
<script>
/*
* new http: fetch
* 一个基于promise的请求方法,更简单,更便捷
*/
let url = "http://jsonplaceholder.typicode.com/posts";
console.log(fetch(url)); // fetch(url) 相当于new Promise((resolved,reject)=> {})
fetch(url)
.then(response => response.json()) // 解析response对象
.then((data) => console.log(data)) // 打印解析的数据
.catch((err) => console.log("error",err)) // 错误码超过300 不会打印结果
</script>
</body>
</html>
es6 fetch
最新推荐文章于 2024-04-02 22:05:31 发布