Vue数据请求 axios和fetch
2019-05-29 12:55
309 查看
Vue数据请求
数据请求在前端开发中的使用有两种形式
- 使用原生javascript提供的数据请求
- ajax( 四部曲,一般需要我们结合Promise去封装,使用不是很便利,但是效率很高 )
- fetch( 本身结合了Promise,并且已经做好了封装,可以直接使用 )使用格式:
- 使用别人封装好的第三方库
目前最流行的,使用率最高的是 axios
vue中我们最常使用的
vue 1.x 的版本提供了一个封装库 vue-resource , 但是到了vue 2.x版本之后,这个就弃用了
vue-resource使用方法和 axios 相似度在 95%
vue-resouce有jsonp方法,但是axios是没有的vue2.x版本我们最用使用的数据请求是 axios 和 fetch
数据请求的类型
- get
- post
- head
- put
- delete
- option
…
axios vs fetch
axios得到的结果会进行一层封装,而fetch会直接得到结果
举例:
axios
{data: 3, status: 200, statusText: "OK", headers: {…}, config: {…}, …}config: {adapter: ?, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}data: 3headers: {content-type: "text/html; charset=UTF-8"}request: XMLHttpRequest {onreadystatechange: ?, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}status: 200statusText: "OK"__proto__: Object
fetch
Axios总结
- get方法
- A: 无参数
axios.get(url).then(res=>console.log(res).catch(error=>conosle.log(error)) - B: 有参数
axios({
url: ‘http://xxx’,
method: ‘get’ //默认就是get,这个可以省略,
params: {
key: value
}
})
- post
- 注意: axios中post请求如果你直接使用npmjs.com官网文档, 会有坑
- 解决步骤:先设置请求头
- 实例化 URLSearchParams的构造器函数得到params对象
- 使用params对象身上的append方法进行数据的传参
// 统一设置请求头axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';let params = new URLSearchParams()// params.append(key,value)params.append('a',1)params.append('b',2)axios({url: 'http://localhost/post.php',method: 'post',data: params,headers: { //单个请求设置请求头'Content-Type': "application/x-www-form-urlencoded"}}).then(res => {console.log( res )}). catch( error => {if( error ){throw error}})
Fetch总结
- get
fetch('http://localhost/get.php?a=1&b=2').then(res=> res.text()) // 数据格式化 res.json() res.blob().then(data =>console.log( data ).catch(error => {if( error ){throw error}})// 注意事项:// A: fetch 的 get 请求的参数是直接连接在url上的, 我们可以使用Node.js提供的url或是qureystring模块来将// Object --> String//B: fetch 的请求返回的是Promise对象,所以我们可以使用.then().catch(),但是要记住.then()至少要写两个, 第一个then是用来格式化数据的,第二个then是可以拿到格式化后的数据// 格式化处理方式有fetch('./data.json').then(res=>res.json()) //res.text() res.blob().then( data => console.log(data)).catch( error => console.log( error ))
- post
post() {fetch('http://localhost/erjieduan/post.php', {method: 'post',headers: new Headers({ //解决跨域'Content-Type': "application/x-www-form-urlencoded"}),body: new URLSearchParams([['a', 2],['b', 1]]).toString()})1c6f4.then(res => res.text()).then(data => console.log(data)).catch(error => {if (error) throw error})},
fetch文档
https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch#进行_fetch_请求fetch请求博客
https://blog.csdn.net/hefeng6500/article/details/81456975
模拟假数据
- mock.js
- json-server( 启动一个api接口服务器 )
相关文章推荐
- vue中使用vue-resource、axios、fetch-jsonp请求数据
- Vue 请求数据(axios)
- Vue项目之Axios数据请求
- 使用vue-cli脚手架 与 axios 请求数据 配置代理进行跨域访问数据问题
- Vue用axios请求数据proxyTable解决跨域问题
- vue使用axios异步请求后端数据的使用
- vue项目及axios请求获取数据
- vue 使用axios 跨域请求数据的问题
- vue-cli2.x简单的跨域请求demo(fetch、axios、proxyTable)
- vue axios数据请求及vue中使用axios的方法
- vue 使用axios 跨域请求数据的问题
- VUE系列三:实现跨域请求(fetch/axios/proxytable)
- vue 的axios请求数据2种方式 很重要
- vue跨域 axios请求数据
- Vue 请求数据 axios , vue-resource
- Vue项目之Axios数据请求
- VUE用AXIOS向后台请求数据
- vue使用axios发送数据请求
- webpack4.0.1_vue脚手架的项目与json-server结合,vue开发前端时用json-server模拟数据,fetch的请求代码
- vue中的axios.post使用json数据传输,出现请求头字段内容类型是不被允许的情况的解决方案