什么跨域的原理和由来啊自己去百度。
旨在解决问题
使用的前提是后台使用的SpringMVC4.2+
//测试H5POST方式的接口
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/testH5", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
@ResponseBody
public void testH5(@RequestBody String status,HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println(status);
System.out.println(request.getHeader("token"));
Writer w = response.getWriter();
Map<String,Object> paramMap=JsonAndMap.jsonTurnSimpleMap(status);
w.write((String)paramMap.get("name"));
}
JS
$.ajax({
type:"POST",
url:url2,
headers:{
'token':token
},
data: '{"name":"dd"}',
contentType: false,
success:function(data) {
}
});