默认发送GET请求
fetch('http://example.com/movies.json')
.then(response => response.json()) //必须要先进行json转换
.then(data => console.log(data));
带参数的GET请求:
var id=1
fetch(`https://www.easy-mock.com/mock/5h/getmsg?id=${id}`)
.then(response => response.json())
.then(data => console.log(data));
发送POST请求
var data={
id:'1',
}
fetch('https://www.easy-mock.com/mock/5/fetch/postmsg',{
method:'POST',
body:data //表示传递一个参数 id=1
})
.then(response => response.json())
.then(data => console.log(data));
fetch发送请求的方法
最新推荐文章于 2024-07-29 10:57:56 发布