springboot测试跨域的例子

  1. 这是前端
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#b01").click(function(){
  htmlobj=$.ajax({url:"http://localhost:8101/admin/edu/teacher/1/3",async:false});
  $("#myDiv").html(htmlobj.responseText);
  });
});
</script>
</head>
<body>
 
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="b01" type="button">改变内容</button>
 
</body>
</html>

2.这是后端

@Api(description="讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
//@CrossOrigin(allowCredentials="true",maxAge = 3600)
public class TeacherAdminController {

    @Autowired
    private TeacherService teacherService;


    //模拟登陆
    // {"code":20000,"data":{"token":"admin"}}

    @ApiOperation(value = "获得登陆admin数据")
//    @GetMapping("login")
    @RequestMapping(value = "login",method = RequestMethod.GET)
//    @CrossOrigin
    public R login(){
        return R.ok().data("token","admin");
    }
  1. 如果不加@CrossOrigin,就是
//@CrossOrigin(allowCredentials="true",maxAge = 3600)

会报错:

jquery-latest.js:9631 Failed to load http://localhost:8101/admin/edu/teacher/1/3: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:9528' is therefore not allowed access.
send @ jquery-latest.js:9631
  1. 如果加了@CrossOrigin就好了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值