IT/Javascript&Jquery
[javascript, ajax] 함수에서 ajax 결과값이 반환되지 않을 때_async 옵션
Remily
2023. 10. 19. 13:00
반응형
서론
변수를 받아 특정 칼럼을 업데이트할 목적으로
function update_column(){
let event = false;
$.ajax({
url: "주소",
data: {'변수값': 변수값},
type: 'POST',
dataType: 'JSON',
success: function(res){
if(res){
const code = res['resultCode'];
const msg = res['resultMsg'];
event = true;
} else{ alert("회신내용 없음"); }
}, error: function(xhr, textStatus, errorThrown){ alert("jqXHR:"+xhr+"\ntextStatus:"+textStatus+"\nerrorThrown:"+errorThrown); }
});
return event;
}
이렇게 함수를 만들었는데
ajax 통신을 성공해도 함수의 반환값은 false 였습니다.

결론
결론만 말하자면 ajax는 비동기가 Default이기 때문에
async 속성을 false 해주어야 동기식으로 처리를 합니다.
function update_column(){
let event = false;
$.ajax({
url: "주소",
data: {'변수값': 변수값},
type: 'POST',
dataType: 'JSON',
async: false,
success: function(res){
if(res){
const code = res['code'];
const msg = res['msg'];
event = true;
} else{ alert("회신내용 없음"); }
}, error: function(xhr, textStatus, errorThrown){ alert("jqXHR:"+xhr+"\ntextStatus:"+textStatus+"\nerrorThrown:"+errorThrown); }
});
return event;
}
오늘 또 한 건 배웠습니다.

반응형