- 这是前端
<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");
}
- 如果不加@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
- 如果加了@CrossOrigin就好了。