• react-fetch数据发送请求


    在一个项目中,数据的请求发送数据是最为重要的,不可能我们的数据都是自己进行编写的

    在react中官方推荐使用的方法是fetch。当然它里面也可以使用vue中的axios请求数据,jQuery的$.ajax 以及元素ajax

    下面重点说一下fetch

    get方法非常简单,

    1 componentDidMount() {
    2         fetch('http://127.0.0.1:8100/getAaa')
    3             .then(res=>res.json())
    4             .then(json=>this.setState({list: json}))
    5 }

    post方法相对于get有点复杂,

     1 add() {
     2         fetch('http://127.0.0.1:8100/getDel',{
     3             method:'post',//改成post
     4             mode: 'cors',//跨域
     5             headers: {//请求头
     6                 'Content-Type': 'application/x-www-form-urlencoded'
     7             },
     8             body:"..."//向服务器发送的数据
     9         })
    10             .then(res=>res.json())
    11             .then(json=>{console.log(json)})
    12 }

    以上就是关于react fetch两种使用方法

    有什么不足的或者不对的欢迎大家指出

  • 相关阅读:
    Mybatis与Spring集成
    Mybatis 多对多
    Mybatis表关联多对一
    Mybatis表关联一对多
    Mybatis增删改查(CURD)
    Mybatis接口注解
    MyBatis环境配置及入门
    MyBatis教程
    Spring JDBC StoredProcedure类示例
    Spring JDBC SqlUpdate类示例
  • 原文地址:https://www.cnblogs.com/nixu/p/9523314.html
Copyright © 2020-2023  润新知