qq_57881666 2023-12-24 22:38 采纳率: 66.7%
浏览 13
已结题

axios跨域,get获取后端数据,报错400

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); }); },   
            },

  • 写回答

1条回答 默认 最新

  • 月亮与山与草 2023-12-25 10:23
    关注

    400是参数错误,跟后台对一下参数类型

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月3日
  • 已采纳回答 12月26日
  • 创建了问题 12月24日