axios GET and POST data request

axios

  1. Third-party library package
  2. https://www.npmjs.com/package/axios
  3. Feature
    1. It was created in the browser is a browser object
    1. It is the underlying Node.js module implements the http
    1. Support Promise
    1. You can intercept the request and response
    • Function: loading loading effect, login interception
    1. Conversion request and response data
    1. Automatically converted to JSON data
    1. Client support to prevent XSRF
    1. automatically encapsulated data axios
  1. use
    • Mock analog data request
      • Requirements: Must be a good return and back-end data communication field
      • mock.js generation
        • mock directory
      • jsonplaceholder
      • Copies of similar data line
        • copy response
    • Request back-end interface
axios-get request:

axios.get (url, configuration items) .then (). catch ()

example:

axios.get(`URL`,{
      params: { //get请求携带参数
      a: 1,
      b: 2
    }
  }).then( res => {
    console.log( res )
  }).catch( error => console.log( error ))
axios-post requests:
  • Request header must be set

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

axios.post (url, configuration) .then (). catch ()

example:

axios.post(`URL`,{
    username: '张三',
    password: '123'
  }).then( res => {
    console.log( res )
  }).catch( err => console.log( err ))

Guess you like

Origin www.cnblogs.com/zengfanjie/p/11721366.html