网页解决跨域问题

1、后台

//预警值
@CrossOrigin
@RequestMapping("/selectT")
//String callback 这个值需要跟页面的jsonp的值对应
void selectT(HttpServletResponse response, String callback) throws IOException {
//设置编码保证前端收到的中文不是???问号
response.setContentType("text/html;charset=utf-8");
// response.setHeader("Access-Control-Allow-Origin", "jsonp");
thresholdService service = new thresholdServiceImp();
threshold threshold = new threshold();
threshold = service.selectThers(1);
JSONObject jsonObject = new JSONObject();
jsonObject.put("threshold", threshold);
//拼接
response.getWriter().print(callback+"("+jsonObject.toString()+")");

}

2、页面Ajax写法

$.ajax({
type:"get",
url:"http://IP(服务器ip地址)/threshold/selectT",

dataType:"JSONP",
jsonp:"callback",
processData: false,
success:function(data){
console.info(data);
$("#fazhi").html(data.threshold.state);
},
error:function(){
alert("数据加载异常!");
}
});

转载于:https://www.cnblogs.com/itxiaoxia/p/11446804.html

你可能感兴趣的:(网页解决跨域问题)