예를 들어 $.getJSON을 이용하여 학생명단을 반환하는 함수 A()가 있을경우, A함수를 호출하게 되면 A에서는 Null값을 반환하게 된다.
이유는 $.getJSON을 이용하여 학생명단을 가지고 오는동안 프로그램이 멈춰있는것이 아니라 $.getJSON을 실행만 시켜놓고 다음 명령어를 계속 진행하게 된다. 따라서 이를 기다리게 하는 방법을 찾아야 한다.
$.getJSON() 은 기본적으로 $.ajax설정에서 ‘async’가 true값으로 되어 있다. 따라서 이를 false로 바꿔주면 위의 문제는 해결된다.
$.ajaxSetup({
async: false
});
* 아래는 ajaxSetup에서의 async 설명원문(http://api.jquery.com/jQuery.ajax/)
asyncBoolean
Default: true
By default, all requests are sent asynchronous (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: “jsonp” requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

18 Replies to “jQuery : Ajax 동기화 $.ajaxSetup({ async: false })”