fetch

1.什么是fetch

相当于promise 必须写两个then 第一个then返回状态码 返回成json格式 第二个then返回json数据

2.使用方法

 $ npm install fetch-ie8 --save

fetch的用法

get 请求

ferch("json/1.json").then(res =>{
return res.json() //json格式
// return res.text() 文本格式
}).then(res =>{
console.log(res)
}).catch(err={
console.log(err)
}) //post 请求
ferch("json/1.json",{
method :"post",
header:{
"Content-Type":"application/x-www-form-urlencodeed"
},
body:"name=key&age=100",
}).then(res=>res.json()).then(=>{
console.log(res)
})
//post 请求第二种
ferch("json/1.json",{
method :"post",
header:{
"Content-Type":"application/json"
},
body:JSONN.stringfy({
name :"efa",
age :100
})
}).then(res=>res.json()).then(=>{
console.log(res)
})

post 请求

//post 请求第一种
ferch("json/1.json",{
method :"post",
header:{
"Content-Type":"application/x-www-form-urlencodeed"
},
body:"name=key&age=100",
}).then(res=>res.json()).then(=>{
console.log(res)
})
//post 请求第二种
ferch("json/1.json",{
method :"post",
header:{
"Content-Type":"application/json"
},
body:JSONN.stringfy({
name :"efa",
age :100
})
}).then(res=>res.json()).then(=>{
console.log(res)
})

fetch的优点和缺点

优点:
  1. 语法简洁,更加语义化
  2. 基于标准 Promise 实现,支持 async/await
  3. 同构方便,更加底层,提供的API丰富(request, response, body , headers)5. 脱离了XHR,是ES规范里新的实现方式
缺点:
1. fetch只对网络请求报错,对400,500都当做成功的请求,服务器返回 400,500 错误码时并不会 reject。
2. fetch默认不会带cookie,需要添加配置项: credentials: 'include'。
3. fetch不支持abort,不支持超时控制,造成了流量的浪费。
4. fetch没有办法原生监测请求的进度,而XHR可以

axios

1、什么是axios

第三方库,专门用来做数据请求

2、使用方法

  • 使用 npm:
    npm install axios
  • 使用cdn
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

3、axios使用

get请求
axios.get("json.json").then(res=>{
console.log(res.data)
})
post请求
//post   x-www-form-urlencoded
axios.post("json.json","name=key&age=100").then(res=>{
console.log(res.data)
}) //post application/json
axios.post("json.json",{
name:"key",
age:"100"
}).then(res=>{
console.log(res.data)
})

axios几乎完美

  • 支持浏览器和node.js
  • 支持promise
  • 能拦截请求和响应
  • 能转换请求和响应数据
  • 能取消请求
  • 自动转换JSON数据
  • 浏览器端支持防止CSRF(跨站请求伪造)

axios练习

 <div id="box">
<button @click="handleClick()">axios</button> <dl v-for="data in dataList" :key="data.id">
<dt><img :src="data.img" alt=""></dt>
<dd>
<h2>{{data.nm}}</h2>
<h3><span>{{data.wish}}</span>人想看</h3>
<h4>{{data.star}}</h4>
<h5>{{data.rt}}上映</h5>
</dd>
<input type="button" value="想看">
</dl> </div>
new Vue({
el:"#box",
data:{
dataList:[]
},
methods: {
handleClick(){
axios.get("maoyan.json").then(res=>{
console.log(res.data.coming)
this.dataList= res.data.coming
})
}
},
})
效果图

vue 信使 ------fetch、axios的更多相关文章

  1. VUE系列三:实现跨域请求(fetch/axios/proxytable)

    1. 在 config/index.js 配置文件中配置proxyTable 'use strict' // Template version: 1.3.1 // see http://vuejs-t ...

  2. 05 . Vue前端交互,fetch,axios,以asyncawait方式调用接口使用及案例

    目标 /* 1. 说出什么是前后端交互模式 2. 说出Promise的相关概念和用法 3. 使用fetch进行接口调用 4. 使用axios进行接口调用 5. 使用asynnc/await方式调用接口 ...

  3. vue中的axios封装

    import axios from 'axios'; import { Message } from 'element-ui'; axios.defaults.timeout = 5000;axios ...

  4. vue中使用axios

    1.结合vue-axios使用 vue-axios是按照vue插件的方式去写的,那么结合vue-axios就可以使用Vue.use()这个方法import axios from 'axios' imp ...

  5. vue全局使用axios插件请求ajax

    vue全局使用axios插件请求ajax Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方宣布停止更新vue-resource,并推 ...

  6. vue创建路由,axios前后台交互,element-ui配置使用,django contentType组件

    vue中创建路由 每一个vue组件都有三部分组成 template:放html代码 script:放js相关 style:放css相关 vue中创建路由 1.先创建组件 Course.vue 2.ro ...

  7. vue全局使用axios的方法

    在vue项目开发中,我们使用axios的二次封装,很多人一开始使用axios的方式,会当成vue-resoure的使用方式来用,即在主入口文件引入import VueResource from 'vu ...

  8. vue中采用axios发送请求及拦截器

    这几天在使用vue中axios发送get请求的时候很顺手,但是在发送post请求的时候老是在成功的回调函数里边返回参数不存在,当时就纳闷了,经过查阅资料,终于得到了解决方案,在此做一总结: 首先我们在 ...

  9. vue项目对axios的全局配置

    标准的vue-cli项目结构(httpConfig文件夹自己建的): api.js: //const apiUrl = 'http://test';//测试域名,自己改成自己的 const apiUr ...

随机推荐

  1. .net core 2.2 中IHttpClientFactory的使用

    在.net core中使用HttpClient请求api,有很多资源的问题,比如使用using的时候,虽然可以释放资源,但是套接字(socket)也不会立即释放,所以.net core2.1中,新增了 ...

  2. final与 static的区别;static代码块以及嵌套类介绍

    本篇文章主要分为两个模块进行介绍:1.final,staic,static final之间的异同:2. static 模块:3.嵌套类的概念 1.final,staic,static final之间的 ...

  3. NumPy排序

    numpy.sort()函数 该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法 使用numpy.sort()方法的格式为: numpy.sort(a,axis,kind,orde ...

  4. Node.js 官方文档中文版

    这目录也是醉了 . 列出跟没列出没两样

  5. 启动Ubuntu的时候出现黑屏的情况

    在启动Ubuntu的时候出现黑屏的情况,是因为升级了内核导致显卡不兼容,启动的时候应该告诉内核不要加载显卡: 在进入系统选择时按e进入编辑 在quiet splash 后面添加 nomodeset 再 ...

  6. 线性最长cover(无讲解)

    #include<bits/stdc++.h> using namespace std; ; int n,f[maxn],cover[maxn],R[maxn]; char str[max ...

  7. Electron使用electron-packager打包记录

    1.使用 JavaScript, HTML 和 CSS 构建跨平台的桌面应用 2.下载https://github.com/electron/electron-quick-start中的示例 3.在示 ...

  8. day04_IDEA、方法

    day04_IDEA.方法 1.快捷输入 psvm:public static void main(String[] args){ } sout:System.out.print("&quo ...

  9. mybatis 源码分析中的知识点

    1. resultMap 和 resultType  之间的优劣 resultMap: 在联合查询的时候, 可以不用写Join (因为在resultMap 的定义里面已经写了这些东西了<asso ...

  10. Spring-data-Jpa项目搭建

    传送门:Spring Data 学习   Spring Data 开发环境搭建   Spring-data-jpa详解 简介 Spring Data是什么    Spring Data是一个用于简化数 ...