React background management system-ajax request encapsulation
1. Create a new folder util, create a new mm.jsx file inside util
2. Use ajax inside jquery to send the request, call the promise back, and return a promise object
-
request(param){
-
return new Promise((resolve, reject) => {
-
$.ajax({
-
type : param.type || 'get',
-
url: param.url || '',
-
dataType : param.dataType || 'json',
-
data : param.data || null,
-
success : res => {
-
// Data request succeeded
-
if(0 === res.status){
-
typeof resolve === 'function' && resolve(res.data, res.msg);
-
}
-
// No login status, mandatory login
-
else if(10 === res.status){
-
this.doLogin();
-
}
-
else{
-
typeof reject === 'function' && reject(res.msg || res.data);
-
}
-
},
-
error : err => {
-
typeof reject === 'function' && reject(err.statusText);
-
}
-
});
-
});
-
}
3. Page introduction
-
const _mm = new MUtil();
4. Use, incoming parameters
-
// Home page statistics
-
getHomeCount(){
-
return _mm.request({
-
url: '/manage/statistic/base_count.do'
-
});
-
}
ON. Posted 2020-04-16 19:56 Six, hc read ( ... ) Comments ( ... ) edit collections
1. Create a new folder util, create a new mm.jsx file inside util
2. Use ajax inside jquery to send the request, call the promise back, and return a promise object
-
request(param){
-
return new Promise((resolve, reject) => {
-
$.ajax({
-
type : param.type || 'get',
-
url: param.url || '',
-
dataType : param.dataType || 'json',
-
data : param.data || null,
-
success : res => {
-
// Data request succeeded
-
if(0 === res.status){
-
typeof resolve === 'function' && resolve(res.data, res.msg);
-
}
-
// No login status, mandatory login
-
else if(10 === res.status){
-
this.doLogin();
-
}
-
else{
-
typeof reject === 'function' && reject(res.msg || res.data);
-
}
-
},
-
error : err => {
-
typeof reject === 'function' && reject(err.statusText);
-
}
-
});
-
});
-
}
3. Page introduction
-
const _mm = new MUtil();
4. Use, incoming parameters
-
// Home page statistics
-
getHomeCount(){
-
return _mm.request({
-
url: '/manage/statistic/base_count.do'
-
});
-
}