使用前先安装:
yarn add whatwg-fetch es6-promise qs
import 'whatwg-fetch'
import 'es6-promise'
import QS from 'qs'//拼接obj
//导出GET请求
export function fetchget(url) {
let result = fetch(url, {
method: 'GET',
/*
fetch不管在同域还是在跨域的情况下,默认都不携带cookie的,所以那些需要权限验证的请求就无法正常获取到数据,这时候需要配置credentials项,有一下三个选项可添:
omit: 默认值,忽略cookie的发送
same-origin: 表示cookie只能同域发送,不能跨域发送
include: 表示既可以同域发送,也可以跨域发送
*/
credentials: 'include',
headers: {
'Accept': 'application/json, text/plain, */*'
}
})
//有三种方式解析获取到的数据:
//1 json数据 用reponse.json()来解析
//2 xml格式文件 用response.text()来解析
//3 图片文件 用response.blob()来解析
return result.then((response) => response.json())
}
//导出POST请求
export function fetchget(url,paramsObj) {
let result = fetch(url, {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded'
},
body:QS.stringify(paramsObj)
})
return result.then((response) => response.json())
}
直接可以复制封装使用
使用如下:
//在要网络请求的文件中引入封装好的fetch文件
import {fetchget} from '@/dateRequire/hspfetch.js'
//直接调用fetchget()
(async ()=>{
let res=await fetchget('http://localhost:3721/api/user')
console.log(res)
})()