Vue中的fetch-post/get请求

需求: 通过fatch把json文件数据请求回来,

要实现的功能:点击一下button按钮,把数据拿回来,

json文件:

{
    "name":"yiyi",
    "age":100,
    "location":"zhengzhou"
}

get方式:

<body>
    <div id="box">
        <button @click="handleFetch">ajax-fetchGet</button>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{

            },
            methods:{
                handleFetch(){
                    // console.log("111")
                    fetch("./test.json")//同域名同端口号下可以省略域名和端口号
                    .then(res =>{
                        //此时res拿到的是状态码、响应头、拿不到真正的数据
                        console.log(res)
                        //res.json拿到的才是真正的数据
                        return res.json()
                    }).then(res=>{
                        //通过第二个then拿到数据
                        console.log(res)
                    }).catch(err=>{
                    console.log(err)
                    })
                }
            }
        })
    </script>
</body>

 简写方式:(箭头函数简写)

        handleFetch(){
                    // console.log("111")
                    fetch("./test.json")//同域名同端口号下可以省略域名和端口号
                    .then(res =>res.json())
                    .then(res=>{
                        //通过第二个then拿到数据
                        console.log(res)
                    }).catch(err=>{
                        console.log(err)
                    })
                }

结果:

  • 成功请求到数据了走then,失败了走catch; 
  • get方式传参,直接在地址(url路径)上拼接:例如:“?name=yiyi&age=100”
  • json格式分两种: json对象,json字符串 

post方式:(携带发给后端的数据)

  • post请求根据参数格式不同分为两种:表单格式“name=yiyi&age=100”、json字符串格式JSON.parse({name:"yiyi",age:100})

第一种方法:传的参数信息是表单格式:

<body>
    <div id="box">
        <button @click="handleFetch">ajax-fetchPost</button>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{

            },
            methods:{
                handleFetch(){
                    fetch("./test.json",{
                        method:"post",
                        headers:{
                            "Content-Type":"application/x-www-form-urlencoded"
                        },
                        body:"name=yiyi&age=100",
                    }).then(res =>res.json())
                    .then(res=>{
                        console.log(res)
                    }).catch(err=>{
                        console.log(err)
                    })
                }
            }
        })
    </script>
</body>

第二种方式:传的参数信息是json字符串格式:

<body>
    <div id="box">
        <button @click="handleFetch">ajax-fetchPost</button>
    </div>
    <script>
        new Vue({
            el:"#box",
            data:{

            },
            methods:{
                handleFetch(){
                    fetch("./test.json",{
                        method:"post",
                        headers:{
                            "Content-Type":"application/json"
                        },
                        body:JSON.stringify({
                            name:"yiyi",
                            age:100
                        }),
                    }).then(res =>res.json())
                    .then(res=>{
                        console.log(res)
                    }).catch(err=>{
                        console.log(err)
                    })
                }
            }
        })
    </script>
</body>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌一一

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值