虽然罗列这么多,但是我只用过一种:
1、 jsonp
2、 document.domain + iframe
3、 location.hash + iframe
4、 window.name + iframe
5、 postMessage
6、 跨域资源共享(CORS)
7、 nginx代理
8、 nodejs中间件代理
9、 WebSocket协议
example:
ajax跨域请求一般是get请求
1、后台方法:
PrintWriter out = response.getWriter();
String backInfo = callback + "(" + JSONObject.parseObject(JSON.toJSONString(map))+ ")"; // 将json数据封装在callback函数中提供给客户端
2.ajax访问并获取参数,通过jsonp
$.ajax({
url: "http://localhost:9090/student",
type: "get",
dataType: "jsonp",
jsonp: "callback",
success: function (data) {
var result = JSON.stringify(data);//将json数据转化为字符串
$("#text").val(result);
}
});