项目场景:
前后端交互
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>axios基本使用</title>
</head>
<body>
<input type="button" value="get请求" class="get">
<input type="button" value="post请求" class="post">
<!-- 官网提供的 axios 在线地址 -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
document.querySelector(".get").onclick = function () {
axios.get("http://localhost:8080/sss?qq=1")
.then( function (response) {
console.log(response);
},function (err) {
console.log(err);
})
}
</script>
</body>
</html>
问题描述
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
翻译:
已被CORS策略阻止:请求的资源上不存在“Access Control Allow Origin”标头
解决方案:
在后端Controller层增加@CrossOrigin注解