@RequestBody接收参数
前端参数构造
//构造参数的两种方式
//第一种,整一个对象,ajax传参的地方需要使用JSON.stringify(data)。
var data = {
"xxxx":xxxx,
"xxxx":xxxx,
"xxxx":xxxx,
"xxxx":xxxx
}
$.ajax({
type : "post",
url : "/repair/start",
dataType : 'json',
contentType:'application/json',
data : JSON.stringify(data),//这里要处理一下
success : function(data) {
console.info(data);
}
})
//第二种,拼接一个json,ajax传参的地方不需要处理直接传参。
var data = '{"xxxx":"' + xxxx + '","xxxx":"' + xxxx+ '","xxxx":"' + xxxx+'","xxxx":"' + xxxx+ '"}';
$.ajax({
type : "post",
url : "/repair/start",
dataType : 'json',
contentType:'application/json',
data : data,
success : function(data) {
console.info(data);
}
})
java后端接收,必须为post请求
@RequestMapping(value = "/start",method= RequestMethod.POST)
public ResultBean<Map> repairStart(@RequestBody Map<String, Object> map){
//这个map,就是传过来的数据
String xxxx= (String) map.get("xxxx");
return null;
}
//一般传数据都是POST传哈,后台代码不要忘了加上POST。
Get请求接受Map参数
URL:
http://127.0.0.1:1221/save?campaignid=12345&devicetype=iPhone7,2&osversion=ios8.1.2&ip=61.135.152.218&click time=1440486732816&idfa_md5=749AB3B68632680660D776891751E812&IMP=E575B F2F8F39F1F062EFC5BA9E8744DAB730614DD5662C0A82C793A7AFEA3365C336F15E 125B1BB61BDDF9123C6C77725678FB46259836835DECCBC26597D042&ad_id=12345 6&creative_id=54321&brand=&devicetype=ios&osversion=ios11.4.1&caid=43e260c0cd69 b81073173045939e5947
后台:
@GetMapping("/save")
public String save(@RequestParam Map<String, String> reqMap) {
log.info("接收数据 -> {}", JSONObject.toJSONString(reqMap));
return "{\"result\":\"OK\", \"Code\":\"0\"}";
}