Access to XMLHttpRequest at ‘http://localhost:8001 springBoot + vue请求显示跨域问题,

Access to XMLHttpRequest at 'http://localhost:8001/****/****/download' from origin 'http://localhost:9528' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

在这里插入图片描述
请求js 代码:

 downloadStudent() {
    return axios.get(process.env.VUE_APP_BASE_API + `/student/query/download`, { responseType: 'blob' })
  }

后端没问题,可以显示查询出来的信息。

 /**
     * 导出所有学生信息
     * 通过Excel导出
     */
    @GetMapping("/****/****")
    public void download(HttpServletResponse response){
        tbSchoolService.download(response);
    }

原因:由于设置了权限框架,该axios请求被拦截。造成跨域的问题

解决方法:设置请求头
在request.js 文件设置
在这里插入图片描述

import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'


// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    if (store.getters.token) {
      config.headers['X-Token'] = getToken()
      config.headers['Authorization'] = getToken()
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

在这里插入图片描述
在编写vue,页面添加

import { getToken } from '@/utils/auth'

请求方法,添加 getToken()

  studentApi.downloadStudent(getToken()).then(res => {}

后端几乎不用修改

请求结果:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,你遇到了CORS(跨域资源共享)的问题。CORS是一种浏览器安全机制,用于限制跨域请求。当你的前端应用(例如Vue.js)从一个域名(例如localhost:9090)向另一个域名(例如localhost:9091)发送请求时,浏览器会执行CORS检查,以确保服务器允许该请求。 在你的情况下,你的前端应用从localhost:9090向localhost:9091发送请求,但是由于CORS策略的限制,该请求被阻止了。错误信息中提到了一个重定向(redirect)的问题,这是因为在发送实际请求之前,浏览器会发送一个预检请求(preflight request)来检查服务器是否允许该请求。由于重定向不被允许的CORS策略,预检请求被阻止了。 要解决这个问题,你可以在后端服务器上进行配置,允许跨域请求。具体的配置方法取决于你使用的后端框架和服务器。以下是一些常见的解决方法: 1. 在后端服务器上设置CORS头部:在后端服务器的响应中添加CORS头部,允许来自前端应用的跨域请求。具体的设置方法取决于你使用的后端框架和服务器。以下是一个示例代码,使用Node.js和Express框架: ```javascript const express = require('express'); const app = express(); app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9090'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); next(); }); // 处理其他路由和请求 app.listen(9091, () => { console.log('Server is running on port 9091'); }); ``` 2. 使用代理服务器:在开发环境中,你可以使用代理服务器来绕过CORS限制。例如,你可以使用webpack-dev-server的proxy配置来将请求代理到后端服务器。具体的配置方法取决于你使用的开发工具和服务器。 3. 使用CORS插件:如果你使用的是特定的后端框架,例如Spring Boot,你可以使用相应的CORS插件来简化配置。例如,对于Spring Boot,你可以使用spring-boot-starter-web包中的CorsFilter来处理CORS问题。 请注意,为了安全起见,你应该仅允许来自受信任的域名的跨域请求,并且只允许必要的HTTP方法和头部。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值