简介:
jquery ajax通用工具类封装,意在解决重复代码轮子问题,欢迎使用
cdn jquery:
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
封装工具类:
function ajaxRequest(url, type, data, successCallback, errorCallback) {
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
if (successCallback) {
successCallback(response);
}
},
error: function(xhr, status, error) {
if (errorCallback) {
errorCallback(xhr, status, error);
}
}
});
}
使用方法:
ajaxRequest('/api/user', 'GET', null, function(response) {
console.log(response);
}, function(xhr, status, error) {
console.error(status, error);
});