vue axios跨域的get和post的使用

版权声明:不忘初心!砥砺前行,与君共勉。 https://blog.csdn.net/m0_37836194/article/details/80375391

1.首先在main.js中引用axios。

import axios from 'axios';
Vue.prototype.$http = axios;
axios.defaults.baseURL = 'http://localhost';

2.get和post的用法

getData () {
      console.log('-------getData')
        let that = this;
that.$http.get
('http://localhost:8080/MySpring').then(function (response) {
        that.serverData = response.data
        console.log(response.data)
      }).catch(function (error) {
        console.log(error)
      })
    },
    post () {
let param = new URLSearchParams();
        param.append("name", "admin");
        param.append("sex", "男");
        param.append("age", 11);

       this.$http.post('http://localhost:8080/MySpring', param, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }).then(function (response) {
        console.log(response)
      }).catch(function (err) {
        console.log(err)
      })
    }

猜你喜欢

转载自blog.csdn.net/m0_37836194/article/details/80375391