前端连接后端时遇到跨域问题
#开发环境
使用ajax连接后端,后端使用springboot框架
#问题
Access to XMLHttpRequest at 'http://10.115.119.255:8080/salary' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
#解决办法
在后端函数前面加入
@CrossOrigin
,解决跨域问题,如下图所示:
#前端代码
$.ajax({
url: "http://10.115.119.255:8080/salary", //提价的路径
type: "post", //提交方式
data: data_input,
contentType: 'application/json;charset=UTF-8',
dataType: "JSON", //规定请求成功后返回的数据
success: function (data) {
var state=data;
console.log(state);
var h = document.createElement("li");
father.appendChild(h);
h.innerHTML = "个人所得税:"+data;
},
error:function ( ) {
console.log("error")
}
})
版权声明:本文为summer_xm原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。