Vue中利用fetch请求ajax
我们来写一个demo,请求URl:https://m.maizuo.com/gateway?cityId=440300&pageNum=1&pageSize=10&type=1&k=9532407中的数据;
首先新建一个实例,在实例中写一个空的数组用于存储请求回来的json数据,方便将数据渲染到页面上;
接下来在methods中写一个方法,在方法中定义一个请求函数fetch,fetch语法如下
/*
fetch(url请求路径,{
method: ''(GET/POST),
headers: {
请求头的密匙
}
}).then(res=>{
return res.数据的格式 //(json() text() arrayBuffer() blob() formData())
}).then(res=>{
在此处处理数据
})
*/
接下来在钩子函数created中调用这个方法
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width