js中函数请求外部系统后台方法时产生跨域问题(不在同一ip,不在同一端口,不用同一协议,满足其一构成跨域)
jquery get 能轻松解决跨域问题,post不能跨域
function test(){
//第一种形式
$.ajax({
type : "GET",
url : "http://localhost:8083/sinosoft-webmodule-cultureCity/cultureManager/CultureManager/test.do?callback=?",
dataType : "jsonp",
jsonp: 'jsoncallback',
success : function(data){
alert(data['info'][0].id);
// alert(json);
$('#msg_box').html(data);
//return true;
}
});
//第二种形式(需要加callback参数)
$.getJSON('http://localhost:8083/sinosoft-webmodule-cultureCity/cultureManager/CultureManager/test.do?callback=?', function(res){
alert(res['info'][0].id);
$('#msg_box').html(res.responseText);
});
}
服务器端:
public void test(){
String callback =HttpUtil.getString(super.getRequest(), "callback");
String result="{" +
"'status':'200'," +
"'info':" +
"[{'id':'JX018','name':'安贞'}," +
"{'id':'CG028','name':'奥运村'}" +
"]}";
this.response(callback+"("+result+")");
}