当我尝试通过Ajax Post请求向控制器发送数据时,不会填充我的@ModelAttribute对象。但是,当我使用表单发送值时,它可以正常工作。请检查下面的代码, 表格 控制器 JavaScript 序列化表单后,我得到 这肯定是一个非常简单的错误,但我在这里无法弄清楚自己在做什么错...可能是什么问题?<form action="#" id="productForm" th:action="@{/save}" th:object="${product}" method="POST">
<table>
<tbody>
<tr>
<td>Name:</td>
<td><input id="name" type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td>Brand:</td>
<td><input id="brand" type="text" th:field="*{brand}" /></td>
</tr>
<tr>
<td>Made In:</td>
<td><input id="madeIn" type="text" th:field="*{madeIn}" /></td>
</tr>
<tr>
<td>Price:</td>
<td><input id="price" type="text" th:field="*{price}" /></td>
</tr>
</tbody>
</table>
<br />
<button type="submit">Save</button>
<button type="button" onclick="saveWithAjaxPost();">Save with Ajax Post</button>
</form>
@RequestMapping(value = "/saveWithAjaxPost", method = RequestMethod.POST)
@ResponseBody
public ModelMap saveWithAjaxPost(@ModelAttribute Product product) {
ModelMap model = new ModelMap();
service.saveProduct(product);
model.put("MESSAGE", "Success");
return model;
}
function saveWithAjaxPost(){
var params = $("#productForm").serialize();
$.ajax({
url:"/saveWithAjaxPost",
type:"POST",
contentType:"application/json",
data:params,
async:true,
dataType:"json",
success:function(response){
$('#productsListDiv').empty();
getAllProcucts();
},
error:function(response){
alert(response.message);
}
});
}
"name=test_name&brand=test_brand&madeIn=test_made_in&price=1000"
。0 个答案:
没有答案