fetch可以更加简单的获取数据,可以看作Ajax的升级版,是基于Promise实现的

1、使用语法

    <script>
        fetch('http://localhost:3000/fdata').then(function(data) {
            return data.text();  // 通过调用text返回promise对象
        }).then(function(data) {
            console.log(data); // 得到真正的结果
        })
    </script>

2、fetch请求参数

  method(String): http请求方法,默认为GET(GET、POST、PUT、DELETE)

  body(String): http的请求参数

  headers(Object): http的请求头,默认为{}

  • (1)get请求方式的参数传递 (传统方式)
    fetch('http://localhost:3000/books?id=123', {
                method: 'get'
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • get请求方式的参数传递 (Restful方式)
    fetch('http://localhost:3000/books/123', {
            method: 'get'
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • (2)delete请求方式的参数传递
    fetch('http://localhost:3000/books/789', {
            method: 'delete'
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • (3)post请求方式的参数传递 (字符串类型参数)
    fetch('http://localhost:3000/books', {
            method: 'post',
            body: 'uname=lisi&pwd=123',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • post请求方式的参数传递 (json对象类型参数)
    fetch('http://localhost:3000/books', {
            method: 'post',
            body: JSON.stringify({
                uname: 'kun',
                pwd: '321'
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • (4)put请求方式的参数传递(字符串类型参数)
    fetch('http://localhost:3000/books/768', {
            method: 'put',
            body: 'uname=lisi&pwd=123',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

  • put请求方式的参数传递(json对象类型参数)
    fetch('http://localhost:3000/books/768', {
            method: 'put',
            body: JSON.stringify({
                uname: 'kun',
                pwd: '321'
            }),
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(function(data) {
            return data.text();
        }).then(function(data) {
            console.log(data);
        })

3、fetch响应结果

    fetch('http://localhost:3000/json')
            .then(function(data) {
                return data.json(); // 返回json对象形式
            }).then(function(data) {
                console.log(data);
            })
    fetch('http://localhost:3000/json')
            .then(function(data) {
                return data.text();  // 返回字符串类型
            }).then(function(data) {
                console.log(data);
            })

原文地址:http://www.cnblogs.com/daixiaoyang/p/16860031.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性