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

有什麼不足的或者不對的歡迎你們指出跨域

相關文章
相關標籤/搜索