美文网首页
Fetch数据请求

Fetch数据请求

作者: 1CC4 | 来源:发表于2019-12-30 20:17 被阅读0次

fetch:是一种HTTP数据请求的方式,是XMLHttpRequest的一种替代方案。
fetch:不是ajax的进一步封装,而是原生js
fetch函数:就是原生js使用promise进行的封装,没有使用XMLHttpRequest对象。

Fetch中文文档地址...

语法:

fetch(url,options).then(res => {        
          return res.json();          
        }).then(data =>{
            console.log(data);  
        }).catch( err => {
            console.log(err);
        })
  • 第一个参数:URL地址
  • 第二个参数:options可选项
  • 使用JavaScript中Promise对象处理回调

示例:

    <div>
        <h2>fetch-es6异步通讯</h2>
        <p id="message"></p>
    </div>
    <script>
        const pElemnt = document.getElementById('message');
        const url = 'http://www.warmtel.com:8089/api/list';
        const options = {
            method: 'GET',
            headers: {
                'content-type':'application/x-www-form-urlencoded'
            }
        } 
        fetch(url,options).then(res => {
            if(res.ok){
                console.log(res);
                return res.json();
            }else{
                return Promise.reject('请求出错!');
            }
        }).then(data =>{
            console.log(typeof(data));
            pElemnt.innerHTML = JSON.stringify(data);
        }).catch( err => {
            console.log(err);
        })

    </script>

相关文章

  • fetch 数据请求

    安装 使用 npm 安装 whatwg-fetch 和 es6-promisenpm install what...

  • Fetch数据请求

    fetch:是一种HTTP数据请求的方式,是XMLHttpRequest的一种替代方案。fetch:不是ajax的...

  • post 数据发送、接受(node 服务)

    背景:前端用 fetch发请求,在 node服务端获取post请求数据 方案:前后端均需配置 1、前端 fetch...

  • React Native--请求Web端(Java)网络数据

    1.在RN中使用fetch请求外部数据 //请求参数 letformData=newFormData(); for...

  • fetch记录一下子

    1.原生http请求 2.fetch请求 3.上面是封装多得fetch,直接使用的fetch fetch请求对某些...

  • React中的数据请求

    React中的数据请求 xhr jQuery中的$.ajax axios fetch fetch是ES6提供的一个...

  • ajax、axios、fetch

    fetch: 是一种HTTP数据请求的方式,是XMLHttpRequest的一种替代方案。fetch不是ajax的...

  • react 数据请求

    React请求远程数据首先下载: fetch/GET 定义posts 数组 componentDidMount ...

  • Fetch

    fetch是一种HTTP数据请求的方式,是XMLHttpRequest的一种替代方案。fetch不是ajax的进一...

  • ReactNative网络请求

    json字符串形式的fetch请求 form 形式的fetch请求

网友评论

      本文标题:Fetch数据请求

      本文链接:https://www.haomeiwen.com/subject/dhmroctx.html