Ajax实现异步post/get请求
get:
$(document).ready(function () { $.ajax({
//默认异步 url: '/api/DataManager/GetAllShelves', type: 'get', dataType: 'json', success: function (data, status) { var resultJson = jQuery.parseJSON(JSON.stringify(data.result.items)) console.log(status); console.log(resultJson); $.each(resultJson, function (index, item) { var tr; tr = '<th scope = "row" > <input type="checkbox" /></th ><td class="tm-product-name">' + item.shelfId + '</td>' + '<td>' + item.areaNumber + '</td>' + '<td>' + item.orderId + '</td>' + '<td>' + item.boxId + '</td>' + '<td>' + item.shelfState + '</td>' + '<td><a href="#" class="tm-product-delete-link"><i class="far fa-trash-alt tm-product-delete-icon"></i></a></td>'; $("#shelves").append('<tr>' + tr + '</tr>') }) } })
}
post:
$(document).ready(function () { //form表单转jsonobject $.fn.serializeObject = function () { var ct = this.serializeArray(); var obj = {}; $.each(ct, function () { if (obj[this.name] !== undefined) { if (!obj[this.name].push) { obj[this.name] = [obj[this.name]]; } obj[this.name].push(this.value || ""); } else { obj[this.name] = this.value || ""; } }); return obj; }; //点击提交,显示提交结果,然后返回页面 $("#AddGoodBtn").click(function () { var jsonBody = JSON.stringify($("#goodForm").serializeObject()); console.log(jsonBody); $.ajax({ url: "/api/DataManager/AddGood", data: jsonBody, type: 'post', dataType:'json', contentType: "application/json", success: function (data, status) { var resultJson = jQuery.parseJSON(JSON.stringify(data)); alert("结果: \n" + resultJson.result + "\n状态: " + status); window.location.href = "products.html"; } }) }); });