定義:
Fetch()方法是一個(gè)瀏覽器實(shí)現(xiàn)的 web API
Fetch()方法是一個(gè) 基于promise發(fā)起HTTP請(qǐng)求
Fetch()是一個(gè)全局方法
語法:
fetch(url)
fetch (url就斤,optiones)
optiones:傳入一個(gè)對(duì)象夫偶,設(shè)置請(qǐng)求的細(xì)節(jié)
返回值:返回一個(gè)promise
實(shí)例:獲取數(shù)據(jù)的方法2:async和await獲取數(shù)據(jù)
<script>
const url = "https://jsonplaceholder.typicode.com/todos"
let request = fetch(url).then(response=>console.log(response.json()))
</script>
實(shí)例:獲取數(shù)據(jù)的方法1:then()方法的鏈?zhǔn)秸{(diào)用獲取數(shù)據(jù)
const url = 'https://jsonplaceholder.typicode.com/todos'
let request = fetch(url)
.then(response => response.json())
.then(data => console.log(data))