fetch和axios

145 阅读1分钟

fetch:

ES6提供的一个新功能,类似于ajax - 是标准方法

以前原生ajax,是一个设计粗糙的API,配置和调用方式非常繁琐

而且基于事件的异步模型写起来不友好

fetch缺点:兼容性不好,我们可以用caniuse.com这个网站看看哪些技术支持哪些浏览器

1、fetch - get

fetch("路径.json").then((res)=>res.json()).then((res)=>{
				console.log(res.data.films)
                                //res要干什么操作
			})

2、fetch - post

fetch("路径.json",{
		method:"POST",
		headers:{
			"Content-Type":"application/x-www-form-urlencoded"
		},
		body:"name=dy&pwd=123123"
	}).then((res)=>res.json()).then((res)=>{
		//res要干什么操作,同理
	})

axios:

是一个第三方的请求方式,但是比fetch更简单更好用,而且vue作者也非常推荐

1、记得先引入,后使用

语法:

get方式

        axios.get("路径.json","请求消息").then(res=>{
                res.data->拿到数据
        })

2、post方式:

        axios.post("路径.json","请求消息").then(res=>{
		res.data->拿到数据
	})