1.首先在springmvc 中配置好Json(参考博客:)
一:用对象接受
1.前台 Ajax 提交
$('#btn_ok').click(function () {
var product={
productName: $("input[name='product_name']").val(),
productNum: $("input[name='product_num']").val(),
productDiscribe: $('#product_detail').val(),
picture: $("input[name='product_pic']").val(),
inPrice: $("#product_inPrice").val(),
outPrice: $("#product_outPrice").val(),
categoryName: $('#sc_category option:selected').val(),//选中的值
supplierCompany: $('#sc_supplier option:selected').val(),//选中的值
};
console.log(product);
$.ajax({
url: "/generalManager/addProductInfo", //后台url
type: "POST",
dataType: "json",
contentType:"application/json;charset=UTF-8",
data: JSON.stringify(product),
success: function(data) {
},
error: function(er) { //失败,回调函数
// alert('修改错误');
alert(er)
}
});
});
2.后台接收
@RequestMapping(value = "/addProductInfo", method = RequestMethod.POST)
@ResponseBody
public void save(@RequestBody(required = false) Product product) {
System.out.println(product);
}
二:用map类型接受
1.前台 Ajax 提交
$.ajax({
url: "/generalManager/addProductInfo", //后台url
type: "POST",
dataType: "json",
contentType:"application/json;charset=UTF-8",
data: JSON.stringify({
productName: $("input[name='product_name']").val(),
productNum: $("input[name='product_num']").val(),
productDiscribe: $('#porduct_detail').val(),
picture: $("input[name='product_pic']").val(),
inPrice: $("#product_inPrice").val(),
outPrice: $("#product_outPrice").val(),
categoryName: $('#sc_category option:selected').val(),//选中的值
supplierCompany: $('#sc_supplier option:selected').val(),//选中的值
}),
console.log(product);
success: function(data) {
},
error: function(er) { //失败,回调函数
// alert('修改错误');
alert(er)
}
});
});
2.后台接收
@RequestMapping(value = "/addProductInfo", method = RequestMethod.POST)
@ResponseBody
public void kk(@RequestBody Map<String,String> map){
System.out.println("HHHHHHHHHHHHHHH"+map.get("productName"));
}