通过jq进行跨域请求
客户端
$.ajax({
url:'http://127.0.0.1:8082/water-store/store/deliveryman/uploadLocation.do',
data:{
lng:location.lng,
lat:location.lat
},
dataType:"jsonp",
type:'POST',
error:function(XMLHttpRequest, textStatus, errorThrown){
fn(errorThrown);
},
success:function(data){
fn(undefined, data);
}
})
服务器端
@RequestMapping("uploadLocation")
@ResponseBody
public String uploadLocation(HttpServletResponse response, String callback,
BigDecimal lng, BigDecimal lat) {
// 需要设置此头部信息
// 指定允许其他域名访问
response.setHeader("Access-Control-Allow-Origin", "*");
// 响应类型
response.setHeader("Access-Control-Allow-Methods", "POST");
// 响应头设置
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
// 返回数据
ViewReturnStateJson json = ViewReturnStateJson.successedText("ok");
return String.format("%s(%s)", callback, JsonUnit.getJsonStr(json));
}