下载axios

    1. yarn add axios
    1. import React, { Component } from 'react'
    2. import axios from 'axios'
    3. export default class index extends Component {
    4. componentDidMount(){
    5. //get请求
    6. axios({
    7. url:'https://httpbin.org/get',
    8. params:{
    9. name:'why',
    10. age:18
    11. }
    12. }).then(res=>console.log(res)).catch(err=>console.error(err))
    13. axios.get('https://httpbin.org/get',{
    14. params:{
    15. name:'apiwhy',
    16. age:28
    17. }
    18. }).then(res=>console.log(res)).catch(err=>console.error(err))
    19. //post请求
    20. axios({
    21. url:'https://httpbin.org/post',
    22. data:{
    23. name:'kobe',
    24. age:18
    25. },
    26. method:'post'
    27. }).then(res=>console.log(res)).catch(err=>console.error(err))
    28. axios.post("https://httpbin.org/post",{
    29. name:'labalon',
    30. age:48
    31. }).then(res=>console.log(res)).catch(err=>console.error(err))
    32. }
    33. render() {
    34. return (
    35. <div>
    36. </div>
    37. )
    38. }
    39. }