http 에러까지 return 해주는 jquery ajax post
마스터욱
1
36
0
0
2024-06-04 14:25:06
/*
ajax_call({
'type ' : 'post',
'url' : '',
'param' : '',
'callback' : (res)=>{
}
});
*/
async function ajax_call(arg = {})
{
//default value
var arg = Object.assign({}, {
'type ' : 'post',
'url' : '',
'param' : {},
'callback' : null,
}, arg);
//console.log(arg); return false;
var type = arg['type '];
var url = arg['url'];
var param = arg['param'];
var callback = arg['callback'];
/*
await new Promise((resolve, reject) => {
$.get(arg['body-url'], {}, function(html){
resolve(html);
$("#fog").hide();
}, "text");
});
*/
$.ajax({
'type' : type,
'url' : url,
'data' : param,
'success' : (res)=>{
//console.log('ajax success', res);
try
{
var data = JSON.parse(res);
//console.log('data', data);
if(data.result)
{
if(typeof callback == 'function')
{
//console.log('callback start', res);
callback(data);
}
}
if(data.msg){
swal({
title : '알림',
text : data.msg,
type : 'warning',
});
}
}
catch(e)
{
//alert("서버에러발생nn"+res);
setTimeout(()=>{
swal({
title : '서버에러',
text : str_cut(res, 700),
type : 'warning',
});
$("#fog").hide();
}, 100);
}
},
'error' : (jqXHR, textStatus, errorThrown)=>{
//alert('서버에러 : ' + jqXHR.status + ' ' + errorThrown);
setTimeout(()=>{
swal({
title : 'Ajax Error',
text : jqXHR.status+"<br />"+errorThrown,
type : 'warning',
});
$("#fog").hide();
}, 100);
}
});
}