JQuery 跨域名访问

function changeDistrict(value){ 

if(value == 0){
$('#transport_node').empty();
$('#transport_node').append('<option value="0">请选择</option>');
return;
}
$('#transport_node').empty();
$('#transport_node').append('<option value="0">加载中...</option>');
$.ajax({
type: 'GET',
url: "http://192.168.33.114:8080/UIDTraceAdmin/transportnode/pagelist/jsonp?callbackFunction=jsonpCallback",
async: false,
dataType: "jsonp",
jsonp: "jsonpCallback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
success: function(o){},
timeout:3000
});
}
//注意:跨域服务端响应返回后调用此函数
function jsonpCallback(result) {
....//具体代码省略 {data:“data”} 直接用 data.data 形式读出字段值。
}

 

跨域服务器处理代码:

[java] view plain copy 在CODE上查看代码片派生到我的代码片
@RequestMapping("/pagelist/jsonp")
public void pagelist(@ModelAttribute TransportNode node,HttpServletRequest httpReq,
HttpSession session,HttpServletResponse response) {

//返回头部设置
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-type", "application/x-javascript;charset=utf-8");
response.setDateHeader("Expires", 0);

String jsonpCallback = httpReq.getParameter("callbackFunction");//jsonp回调函数名
JSONObject resultJson = new JSONObject();
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
node.setRowStart((node.getPage() - 1) * node.getRows() + 1);
node.setRowEnd(node.getPage() * node.getRows());

resultJson.put("transportList", JsonUtils.toJSONList(business.getList(node)));
resultJson.put("success", true);
System.out.println(resultJson.toString());
out.println(jsonpCallback+"("+resultJson.toString()+")");//返回jsonp格式数据
out.flush();
out.close();

} catch (Exception e) {
LogWriter.log("/pagelist/jsonp",e);
try {
resultJson.put("success", false);
} catch (JSONException e1) {
e1.printStackTrace();
}
out.println(jsonpCallback+"("+resultJson.toString()+")");//返回jsonp格式数据
out.flush();
out.close();
}

转载于:https://www.cnblogs.com/displayqi/p/5769540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值