Use axios in vue framework, similar to native ajax call interface

In vue:
①The first way of writing
Insert picture description here
Insert picture description here

	 this.$axios.get('http://wthrcdn.etouch.cn/weather_mini', {
    
    
        params: {
    
    
          city: "北京"
        }
        })
        .then(function (response) {
    
    
        console.log(response);
        })
        .catch(function (error) {
    
    
        console.log(error);
      })

②The second way of writing
Insert picture description here
ps:
The params in the following paragraph must be written as params, not data, because when axios sends a request, params is added to the url string for get requests, and data is added to the request body Yes, for post requests.

	this.$axios({
    
    
          method:'get',
          dataType:'json',
          contentType:"application/json",
          url:'http://wthrcdn.etouch.cn/weather_mini',
          params:{
    
    
            city:"北京"
          },          
      }).then((res)=>{
    
    
        console.log(res);
      })




Need to configure the environment before using axios

①Execute commands in cmd

npm install axios

②You can see this line in package.json
Insert picture description here

③Write these lines in src/main.js
Insert picture description here

import axios from 'axios'

Vue.config.productionTip = false
Vue.prototype.$axios=axios
Vue.config.productionTip = false

④npm run serve can use axios

Guess you like

Origin blog.csdn.net/weixin_44575911/article/details/111329683