前台数据转JSON对象:
/**
*
* @param $myFrom:from表单
* @returns {{}}
*/
function from($myFrom) {
var ebookEntry = {};// 要传递给后台的对象数据
$($myFrom).each(function (i, n) {
ebookEntry[$myFrom[i].name] = $myFrom[i].value.trim();
});
return ebookEntry;
}
前台Ajax发送请求:
$("#addBtn").click(function () {
//序列化表单
var $myFrom = $("#MyForm").serializeArray();
$.ajax({
type: "POST",
url: "/addSaleQ",
data: JSON.stringify(from($myFrom)),//必须
contentType: "application/json;charsetset=UTF-8",//必须
dataType: "JSON",
success: function (data) {
}
});
})
后台接收:
以上为关键代码:没毛病