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两种使用方法

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