axios
- Third-party library package
- https://www.npmjs.com/package/axios
- Feature
-
- It was created in the browser is a browser object
-
- It is the underlying Node.js module implements the http
-
- Support Promise
-
- You can intercept the request and response
- Function: loading loading effect, login interception
-
- Conversion request and response data
-
- Automatically converted to JSON data
-
- Client support to prevent XSRF
-
- automatically encapsulated data axios
- 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
- https://www.showdoc.cc/ back-end rendering template
- post request header request must be set
- Mock analog data request
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 ))