一、fetch使用
fetch 接受两个参数:一个是url,一个是options配置项。
二、Options配置项
当请求头里设置了 Content-Type:application/json 时,请求参数一定是json字符串。
fetch(url,{
method:'POST', // 请求方式 默认Get请求
body:{}, // 请求参数
headers:{ // 请求头
'Content-Type':'application/json' ,
},
credentials:'include' | 'same-origin' | 'omit',
//是否携带cookies凭证,带上 | 同源带上 | 不带上
})
三、返回结果
一般情况下:
fetch(url)
.then(response => response.json())
.then(data => {
console.log(data)
});
非一般情况:后端返回的数据格式是文件流
fetch(url)
.then(response => response.blob())
.then(data => {
console.log(data)
const url = window.URL.createObjectURL(data) // 想要获得url格式使用
});
1187

被折叠的 条评论
为什么被折叠?



