Access to XMLHttpRequest at 'xxx' from origin 'xxx' has been been blocked by CORS policy

本文介绍了解决前端开发中遇到的CORS跨域访问限制问题的方法,通过在SpringBoot后端接口添加@CrossOrigin注解实现跨域请求的支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端出现Access to XMLHttpRequest at ‘xxx’ from origin ‘xxx’ has been been blocked by CORS policy错误
解决方法
在SpringBoot后台接口上加上 @CrossOrigin
例如

import com.example.demo.bean.Admin;
import com.example.demo.service.Imap.AdminService;
import com.example.demo.utils.Message;
import com.example.demo.utils.MessageUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(description = "用户相关接口")
//@CrossOrigin 跨域请求
@CrossOrigin
@RestController
@RequestMapping("/Admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @ApiOperation(value ="查询所有用户",notes="" )
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "请求已完成"),
            @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
            @ApiResponse(code = 401, message = "未授权客户机访问数据"),
            @ApiResponse(code = 403, message = "资源不可用"),
            @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
            @ApiResponse(code = 500, message = "服务器不能完成请求")
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值