美文网首页
vue学习--前端交互(三、接口调用-fetch用法)

vue学习--前端交互(三、接口调用-fetch用法)

作者: 若能遇见 | 来源:发表于2020-04-12 21:52 被阅读0次

1 fetch概述

1.1 基本特性

  • 更加简单的数据获取方式,可以看做是XMLHttpRequest的升级版
  • 基于Promise实现

1.2 语法结构

fetch(url).then(fn2)
              .then(fn3)
              ...
              .catch(fn)

2 fetch的基本用法

fetch('/abc').then(data=>{
   return data.text();
}).then(ret=>{
   // 注意这里得到的才是最终数据
   console.log(ret)
})
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  
  <script type="text/javascript">
    /*
      Fetch API 基本用法
    */
    fetch('http://localhost:3000/fdata').then(function(data){
      // text()方法属于fetchAPI的一部分,它返回一个Promise实例对象,用于获取后台返回的数据
      return data.text();
    }).then(function(data){
      console.log(data);
    })
  </script>
</body>
</html>

3 fetch 请求参数

3.1 常用配置选项

  • method(String):HTTP请求方法,默认为GET
  • body(String):HTTP的请求参数
  • headers(Object):HTTP的请求头,默认为0
fetch('/abc',{
   method:'get'
}).then(data=>{
   return data.text();
}).then(ret=>{
   // 注意这里得到的才是最终的数据
   console.log(ret)
})

3.2 Get请求传递参数

3.3 Delete请求传递参数

3.4 Post请求传递参数

3.4 Put请求传递参数

  <script type="text/javascript">
    /*
      Fetch API 调用接口传递参数
    */

    // GET参数传递-传统URL
    // fetch('http://localhost:3000/books?id=123', {
    //   method: 'get'
    // })
    //   .then(function(data){
    //     return data.text();
    //   }).then(function(data){
    //     console.log(data)
    //   });

    // GET参数传递-restful形式的URL
    // fetch('http://localhost:3000/books/456', {
    //   method: 'get'
    // })
    //   .then(function(data){
    //     return data.text();
    //   }).then(function(data){
    //     console.log(data)
    //   });

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

    // 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请求传参
    // fetch('http://localhost:3000/books', {
    //   method: 'post',
    //   body: JSON.stringify({
    //     uname: '张三',
    //     pwd: '456'
    //   }),
    //   headers: {
    //     'Content-Type': 'application/json'
    //   }
    // })
    //   .then(function(data){
    //     return data.text();
    //   }).then(function(data){
    //     console.log(data)
    //   });

    // PUT请求传参
    fetch('http://localhost:3000/books/123', {
      method: 'put',
      body: JSON.stringify({
        uname: '张三',
        pwd: '789'
      }),
      headers: {
        'Content-Type': 'application/json'
      }
    })
      .then(function(data){
        return data.text();
      }).then(function(data){
        console.log(data)
      });
  </script>

fetch响应结果

响应数据格式

  • text():将返回体处理成字符串类型
  • json():返回结果和JSON.parse(responseText)一样
<script type="text/javascript">
    /*
      Fetch响应结果的数据格式
    */
    fetch('http://localhost:3000/json').then(function(data){
      // return data.json();
      return data.text();
    }).then(function(data){
      // console.log(data.uname)
      // console.log(typeof data)
      var obj = JSON.parse(data);
      console.log(obj.uname,obj.age,obj.gender)
    })
  </script>

相关文章

  • Vue(3)

    前后端交互 前后端交互模式 Promise用法 接口调用-fetch用法 接口调用-async/await用法 接...

  • Vue数据请求

    1 - 前端接口调用方式 原生ajax 基于jQuery的ajax fetch axios 2 - 接口调用-fe...

  • Vue接口调用方式(一)fetch用法

    ✍目录总览: 1. fetch概述 基本特性 fetch是传统ajax的升级版本,是原生js 更加简单的数据获取方...

  • 前端学习笔记二十六-Vue之前后端交互

    一、前后端交互模式 接口调用方式 原生ajax 基于jQuery的ajax fetch axios 异步 Java...

  • 前后端交互

    1 前后端交互模式 1.1接口调用方式: 原生AJAX 基于JQuery的AJAX fetch axios 原生的...

  • 04Vue的前后端交互

    Vue的前后端交互 Promise用法 异步调用 触发异步调用的方式定时任务Ajax事件函数 多次异步调用的依赖分...

  • 谈谈接口错误码

    对于程序员来说,接口是再熟悉不过的了,前端使用后端提供的接口进行交互。为了让前端知道调用的是否产生异常。我们需要定...

  • iOS/Android与前端Vue的交互

    这阵子移动端两个端都和前端做了交互 记录一下 前端调用iOS/Android(以目前前端最流行的框架Vue为例 )...

  • 接口测试

    web 接口测试 vue中的请求接口 F12(右键检查)打开控制台,选择network和XHR 这就是前端页面调用...

  • Vue接口调用方式(三)async/await用法

    async/await用法 1. async/await的基本用法 async/await是ES7引入的新语法,可...

网友评论

      本文标题:vue学习--前端交互(三、接口调用-fetch用法)

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