前台html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSONP DEMO</title>
<script src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
changeDistrict(1);
});
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>');
var mydata={
"method":"testJsonp",
"token":""
}
$.ajax({
type: 'GET',
url: "http://192.168.1.123:80/YJWeixin/control.do",
async: false,
dataType: "jsonp",
data:{"jsonParams":JSON.stringify(mydata),"callbackFunction":"jsonpCallback"},
jsonp: "jsonpCallback",
success: function(o){},
timeout:3000
});
}
function jsonpCallback(result) {
if(result.code!= ""){
$('#transport_node').empty();
$('#transport_node').append('<option value="0">请选择</option>');
var html = '<option value="'+result.code+'">'+result.price+'</option>';
$('#transport_node').append(html)
}else{
$('#transport_node').empty();
$('#transport_node').append('<option value="0">请选择</option>');
}
}
</script>
</head>
<body>
<div style="width:400px;height:300px;border:1px">
<select id="transport_node"> </select>
</div>
</body>
</html>
后台:
/**
* 测试jsonp
* @param req
* @param resp
* @param params
* @return
* @throws Throwable
*/
@HandlerMethod(methodName=FunctionCons.TEST_JSONP,required=false,info=false)
public static String testJsonp(HttpServletRequest req,
HttpServletResponse resp,HashMap<String,String> params) throws Throwable
{
String jsonpCallback = req.getParameter("callbackFunction");
WeixinService ws = (WeixinService) ApplicationContext.getInstance()
.getBean(WeixinService.NAME);
// HashMap<String,Object> res = ws.checkRegionAuthStatus(wxId);
JSONObject json =new JSONObject();
json.put("code","aaa");
json.put("price",1780);
json.put("tickets",5);
return jsonpCallback+"("+json.toString()+")";
}
效果:
原文链接:https://www.f2er.com/json/289395.html