AJax封装避免页面重复代码

很多时候页面经常用到ajax调用数据。但是用的多了,就显得代码乱。也彰显不出我们前端人员的水平。

所以封装很有必要:

function getValueByAjax(method ,requestUrl, params) {
var strRetValue;
$.ajax({
type: method,
url: requestUrl,
contentType: 'application/json',
dataType: "json",
async:false,
data: params,
success: function(data) {
if (data) {
strRetValue = data;

//console.log(data)
}
else {
strRetValue="XXXX!" ;
}

},
error: function(err) {
strRetValue;
}
});
return strRetValue;
}

后期直接调用:

let datas=getValueByAjax("GET",url,"null");

 let datas=getValueByAjax("POST",url,"params");

  

原文地址:https://www.cnblogs.com/agen-su/p/11643364.html