標籤:
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上查看代碼片派生到My 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();
}
JQuery 跨網域名稱訪問