// 注释是重点!!!
$(document).ready(function () {
$('#saveRecordCustomerInfoBtn').click(function () {
var customerId = $('#customerId').val();
var customerName = $('#customerName').val();
if (!customerId) {
showErrTip('0');
} else if (!customerName) {
showErrTip('1');
} else {
type = 'POST';
url = '/post_saveRecordCustomer/';
postData = {
"customerId": customerId,
"customerName": customerName
};
callback = function (data) { // callback 一定要在 发起ajax请求的前面,否则提示没有定义
showErrTip(data); // 返回 成功/失败信息
};
requestAjax(type, url, postData, callback); // 发起ajax请求,如果有返回值,则调用回调函数callback即可!
}
})
});
// ajax 请求(封装)
function requestAjax(type, url, requestData, callback) {
$.ajax({
type: type,
url: url,
data: requestData,
dataType: 'json',
success: function (data, tetxStatus) {
ajaxSuccess(data, callback);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
})
}
// ajax请求 成功,返回(调用 发起者的 回调函数)
function ajaxSuccess(data, callback) {
return callback(data);
}
Jq ajax 结果返回,return无效,解决方式!
最新推荐文章于 2023-09-13 11:29:43 发布