카테고리 없음

[ajax] async

HoneyIT 2023. 1. 25. 09:00
반응형

 

$(document).ready(function(){
	fnAjax();
    fnConsole();
}

function fnAjax(){
	$.ajax({
		type : 'POST',
		url : getContextPath() + '/url.com',
		data : $('#frmAjax').serialize(),
		async : false,
		dataType : 'json',
		success : function(data) {
			console.log('ajax hi!');
		},
		error : function(x, error) {
			showMessageBox('서버통신 중 오류가 발생하였습니다.');
			console.log(x);
		},
	}); 
}

function fnConsole(){
	console.log('console hi!');
}

Ajax 호출시 async의 default 값은 true입니다.

async : true 일 경우 비동기식으로 동작하며  ajax의 success결과인 'ajax hi!'가 아닌 console hi!가 먼저 출력됩니다.

async : false 일 경우 동기식으로 동작하여 'ajax hi!'가 먼저 출력됩니다.

반응형