Springboot通过Ajax上传json对象
1.使用@RequestParam
@PostMapping(value = "/search")
@ResponseBody
private String search(@RequestParam("mobile") String mobile) {
System.out.println(mobile);
JSONObject.parse(mobile);
// System.out.println(jsonObj);
JSONObject result = new JSONObject();
result.put("msg", "ok");
return result.toJSONString();
}
$.ajax({
type: "POST",
url: "/user/search",
dataType: 'json',
data: { "mobile": mobile },
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
})
java端接收到的值直接是mobile的值,打印出来直接是一个手机号
System.out.println(jsonString); 的输出结果是:18810888888
2.使用@RequestBody
@PostMapping(value = "/search")
@ResponseBody
private String search(@RequestBody String jsonString) { //@RequestParam("mobile") String mobile
System.out.println(jsonString);
// System.out.println(jsonObj);
JSONObject result = new JSONObject();
result.put("msg", "ok");
return result.toJSONString();
}
$.ajax({
type: "POST",
url: "/user/search",
dataType: 'json',
data: { "mobile": mobile },
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
})
System.out.println(jsonString);的输出结果是 mobile=18810888888