GET http://localhost:8080/api/notice/page 400 (Bad Request)
使用了api来替代后端的IP地址,最后显示是本地地址,但是后端可以收到,但不能获取到后端的数据
main.js
Vue.prototype.$axios = axios.create({
headers:{'Content-Type':'application/json'},
withCredentials:true
});
Vue.prototype.qs = Qs;
axios.defaults.baseURL = '/api';
axios.defaults.timeout=15000;
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.get['Content-Type'] = 'application/json'
vue.config.js
devServer: {
open: true,// 自动打开浏览器
host: "0.0.0.0",// 本地启动
port: 8080,// 端口号
proxy: {
'/api/': { //访问路径,可以自己设置,
target: '', //代理接口,即后端运行所在的端口
//secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, //设置是否跨域
ws: true,
pathRewrite: { //访问路径重写
'^/api': '/'
},
}
}
index.vue
testFetch() {
let url = '/api/notice/page';
fetch(url, { method: 'GET',
headers: { post:{'Content-Type': 'application/json'} ,
get:{'Content-Type': 'application/json'}
},
credentials: 'include',
// 包括跨域请求中的 cookies
})
.then(response => {this.tb = response.data.records,
console.log(response)},
)
.catch(err => { console.error('请求失败', err); }); },
},