20-fetch

Fetch兼容性不好,基于promise,fetch请求默认是不带cookie的,需要设置fetch(url,{credentials:'include'})

image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./lib/vue.js"></script>
</head>
<body>
    <div id="box">
        
         <!-- fetch  兼容性不好 -->
        <button @click = "handleFetch()">ajax-fetch</button>
    </div>
    <script>
        var vm = new Vue({
            el:'#box',
            data:{},
            methods:{
                handleFetch(){
                    fetch("json/test.json?name=xiaoming",{
                        method:'get',    //不写也为get请求
                    })
                        // .then(res=>{
                        //     console.log(res)
                        //     return res.json()
                        // })
                        // .then(res=>{
                        //     console.log(res)
                        // })
                        .then(res=>res.json())
                        .then(res=>{
                            console.log(res)
                        })
                        //res.text()  字符串    res.json()  json对象




                    // post请求-1   

                    // fetch("json/test.json",{
                    //     method:'post',
                    //     headers:{
                    //         "Content-Type":"application/x-www-form-urlencoded"    //请求头
                    //     },
                    //     body:'name=xiaoming&age=100'
                    // }).then(res.json()).then(res=>{console.log(res)}) 

                    // post请求-2   

                    // fetch("json/test.json",{
                    //     method:'post',
                    //     headers:{
                    //         "Content-Type":"application/json"    //请求头
                    //     },
                    //     body:JSON.stringify({
                    //          name:"xiaoming",
                    //          age:18
                    //     })
                    // }).then(res.json()).then(res=>{console.log(res)}) 



                }
            }
        })
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文章是从#fetch使用的常见问题及解决办法复制过来的, 首先声明一下,本文不是要讲解fetch的具体用法,不清楚...
    _____西班木有蛀牙阅读 8,247评论 0 9
  • 参考AJAX 之 XHR, jQuery, Fetch 的对比[https://zhuanlan.zhihu.co...
    合肥黑阅读 8,901评论 0 3
  • Ajax 是什么? 答:Ajax是一种可以在浏览器和服务器之间使用异步数据传输(HTTP请求)的技术。使用它可以让...
    缘自世界阅读 6,980评论 6 14
  • 导读 传递信息到服务器,从服务器获取信息,是前端发展的重中之重,尤其是现在前后端分离的大前提下,前后端的数据交互是...
    Chris_dc阅读 6,521评论 1 9
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,897评论 2 7