1、post请求
(1)、js部分
$.ajax({
type: 'POST',
url: url,
data: param,//参数
dataType:'json',
success: function(data){
//回调函数里面要执行的代码
}
});
var param={
jbfl: ""+encodeURI(jbfl)+"",
wryjbdx: ""+encodeURI(wryjbdx)+"",
wry: ""+encodeURI(wry)+""
};
(2)、后台controller
@RequestMapping(value = "/save")
@ResponseBody
public Object save(HttpServletResponse response) {
/**
* 返回结果map
*/
Map<String, Object> map = new HashMap<String, Object>();
response.setHeader("Access-Control-Allow-Origin", "*");//跨域post请求,必须加上这句//获取从前台传过来的参数(可以使用request.getParameter("参数")直接获取前台传的参数)
PageData requestDate = new PageData(getRequest());
String jbfl=String.valueOf(requestDate.get("jbfl"));//举报分类
String wryjbdx=String.valueOf(requestDate.get("wryjbdx"));//污染源举报对象
String wry=String.valueOf(requestDate.get("wry"));//污染源
map.put("jbfl",jbfl);
map.put("wryjbdx",wryjbdx);
map.put("wry",wry);return map;
}
2、get请求
(1)、js部分
$.ajax({
type:'GET',
url:url,
data: param,//参数
dataType:'jsonp',
jsonp:"callbackparam",//前后台要保持一致
jsonpCallback:"success_jsonpCallback2",
success: function(data){
//回调函数里面要执行的代码
}
});
var param={
jbfl: encodeURI(jbfl),
wryjbdx: encodeURI(wryjbdx),
wry: encodeURI(wry)
};
(2)后台controller
@RequestMapping(value = "/save")
@ResponseBody
public Object save() {
/**
* 返回结果map
*/
Map<String, Object> map = new HashMap<String, Object>();PageData requestDate = new PageData(getRequest());//获取从前台传过来的参数
String callback=String.valueOf(requestDate.get("callbackparam"));//获得jsonp回调函数名的参数名
String jbfl=String.valueOf(requestDate.get("jbfl"));//举报分类
String wryjbdx=String.valueOf(requestDate.get("wryjbdx"));//污染源举报对象
String wry=String.valueOf(requestDate.get("wry"));//污染源map.put("jbfl",jbfl);
map.put("wryjbdx",wryjbdx);
map.put("wry",wry);
JSONObject json=JSONObject.fromObject(map);
return callback +"("+ json + ")";//返回json数据}