vue中请求ajax
fetch
如何使用: "key=v&key2=v"
fetch(url,{
headers:{
"token":localStorage.getItem('token'),
"content-type":"apllication-xxx-urlencoded"
},
method:"GET/POST",
data:{
}
}).then(function(res){
return res.json() //text() arrayBuffer() blob() formData()
}).then(res=>{
//res 才是请求的结果 格式化之后(什么格式取决于 第一个 then 返回的处理方法)
})
问?fetch 如何做兼容
fetch 兼容性差的原因是啥:
使用promise 不支持 ie11及以下的
且,在vue react等脚手架中 配置babel es6转es5,也无法转换promise
使用babel-polyfill 插件来解决
Vue 如何兼容IE(IE9及以上), 使用babel-polyfill 插件来解决
解决fetch 兼容性问题?
1,使用babel-polyfill
2,直接使用 git https://github.com/github/fetch
基于原生的fetch 封装的,当有的浏览器不支持fetch时,转换成普通的xhr对象(内部集成了babel-polyfill)