vue-axios
安装依赖
npm i axios
在main.js文件中声明
import axios from 'axios' Vue.prototype.$axios = axios;//推荐使用
或
Vue.use(axios); //这样可能出现axios is not defined
get请求实例
<template> <div> <button @click="getRequest()">获取后端数据</button> </div> </template> <script> export default { name: "HelloWorld", methods: { getRequest: function() { //axios.get 发起get请求 //参数一 表示请求地址 //参数二 表示配置信息 //参数二里params 表示传递到服务器端的数据,以URL参数的显示拼接在请求地址后面 //参数二里headers 表示请求头 this.$axios.get("http://.../...(这里是请求地址)", { params: {}, headers: {} }) .then(res =>{console.log(res)});//返回从服务器中取到的数据
}
}
}
</script>
post实例
postRequest: function() { //post请求传递三个参数 //参数一 请求地址 //参数二 请求数据 在请求体中传递 //axios默认发送数据是json格式的 //参数三 配置信息 //headers 中有conttent-type:'aplication/json' 所以默认是js格式 this.$axios.post("https:api...请求地址", //请求数据 { userName: 'wangwu', password: '123456' } ) .then(res=> { console.log(response) }); //返回从服务器中取到的数据 }