AJAX跨域请求

JS请求:

 

$(function(){
var d = new Date();
var url = '其他域请求地址';
$.ajax({
    url: url,
    type: 'GET',
    crossDomain: true,
    success: function(data) { 
		alert(data);//处理返回数据
	},
    error: function() { alert('Failed!'); },
	dataType: 'jsonp',
	jsonp:'callbackparam',//向服务器
	jsonpCallback:'success_jsonpCallback'//回调方法名
});

var success_jsonpCallback=function(data){}
});

 JAVA服务器响应:

 

response.setContentType("application/json;charset=UTF-8"); //这里的格式是json
response.setHeader("Cache-Control","no-cache");
String limit = request.getParameter("limit");
String jsoncallbackname = request.getParameter("callbackparam");

PrintWriter out = response.getWriter();
out.print(jsoncallbackname+"("+toJson(result)+")");
out.flush();

你可能感兴趣的:(AJAX跨域)