React的网络请求发送-----React学习(二)

学前知识:

1、React本身只关注于页面,并不包含发送ajax请求的代码

2、前端应用需要通过ajax请求来与后台进行交互

3、react应用中需要集成第三方ajax库

4、常用的ajax库有jQuery和axios

在这里,我们使用axios:


安装依赖:

npm install axios --save

引入axios依赖:

import axios from 'axios'

axios的get操作:

axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response.data);
  })
  .catch(function (error) {
    console.log(error);
  });

axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
  console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

axios的post请求:

axios.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

猜你喜欢

转载自blog.csdn.net/qq_53087870/article/details/120214622