react-fetch数据发送请求

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

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

下面重点说一下fetchios

get方法很是简单,ajax

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有点复杂,json

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

有什么不足的或者不对的欢迎你们指出跨域

相关文章
相关标签/搜索