美文网首页
fetch 拿到的数据为 ReadableStream 格式的处

fetch 拿到的数据为 ReadableStream 格式的处

作者: O无为学长O | 来源:发表于2019-06-25 19:22 被阅读0次

    昨天在使用 fetch 时,发现返回的数据格式为 ReadableStream ,百度之下发现 fetch 默认返回的 body 就是 ReadableStream 数据,需要自己处理成期望的数据格式

    首先,你得知道后端返回的数据格式是什么。

    如果是 json字符串 ,使用 res.json() 方法来将数据转换成 json 格式数据

      fetch('请求地址').then(res=>{
        return res.json();
      }).then(res=>{
        console.log(res); // 这里得到的就是 json 格式数据
      });
    

    如果是普通的字符串,使用 res.text() 方法将数据转换成普通字符串

      fetch('请求地址').then(res=>{
        return res.text();
      }).then(res=>{
        console.log(res); // 这里得到的就是普通字符串格式数据
      });
    

    相关文章

      网友评论

          本文标题:fetch 拿到的数据为 ReadableStream 格式的处

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