How axios requests data

axios

Axios encapsulates ajax to realize asynchronous requests.
Axios is a Promise-based HTTP library that can be used in browsers and node.js

1. Features:
  • Create XMLHttpRequests in the browser
  • In node.js, create an http request
  • Support Promise API
  • Support interception of requests and responses
  • Convert request and response data
  • Cancel request
  • Automatically convert JSON data
  • The client supports defense against XSRF
2. How to use (two):
  • Front-end separation: Introduce the axios.js file in the front-end
  • Module development: install the axios module in the project
    npm install axios
3. Common methods:
  • axios.request(fonfig)
  • axios.get(url[,config])
  • axios.post(url[,config])
  • axios.delete(url[,config])
  • axios.put(url[,config])
4. Examples:
axios({
    
    
    url:'接口地址url',
    method:'get/post/put/delete',
    responseType:'json'
}).then(response =>{
    
      //请求成功后的响应函数
    self.blink=response.data
}).catch(error=>{
    
       //请求失败后的响应函数
    console.log(error)
})

The difference between axios and ajax and their advantages and disadvantages

1. Difference:

Axios is a kind of encapsulation of ajax technology through Promise, just like the encapsulation of ajax by jquery. To
put it simply, ajax technology realizes the refresh of partial data, and axios realizes the encapsulation of ajax. Axios has some ajax, ajax Some axios may not have them. In summary, axios is ajax, and ajax is more than just axios.

2. Advantages and disadvantages:

ajax:

1. It is for MVC programming and does not meet the wave of front-end MVVM

2. Based on the original XHR development, the XHR itself is not clear. There is an alternative to fetch. The entire project of jquery is too large. It is very unreasonable to use ajax to introduce the entire jquery (it is not reasonable to adopt a personalized packaging scheme and not enjoy the CDN service) )

3. Ajax does not support the browser's back button

4. Security issues Ajax exposes the details of interacting with the server

5. The support for search engines is relatively weak

6, the abnormal mechanism of destroying the program

7. Not easy to debug

axios:

1. Create an http request from node.js

2. Support Promise API

3. The client prevents CSRF (malicious use of websites)

4. Provides some concurrent request interfaces

Guess you like

Origin blog.csdn.net/isfor_you/article/details/114653331