npm i axios
在vue.config.js文件中添加
//开启代理服务器 方式一
devServer: {
proxy: 'http://localhost:5000'
}
//开启代理服务器 方式二
devServer: {
proxy: {
'/prefix': { //请求前缀 以此为前缀的请求都是访问外部
target: 'http://localhost:5000',
pathRewrite: {'^/prefix': ''}, //发送请求时把前缀去掉
ws: true, //用于支持websocket
changeOrigin: true //用于控制请求中的host值
},
'/demo': { //请求前缀 以此为前缀的请求都是访问外部
target: 'http://localhost:5000',
pathRewrite: {'^/demo': ''}, //发送请求时把前缀去掉
ws: true, //用于支持websocket
changeOrigin: true //用于控制请求中的host值
}
}
}
引入 import axios from ‘axios’
发送请求
getStudents(){
axios.get('http://localhost:8080/text.text').then(
response => {
console.log('请求成功了',response.data)
},
error => {
console.log('请求失败了',error.message)
}
)
}