> For the complete documentation index, see [llms.txt](https://shhn0509.gitbook.io/dom/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shhn0509.gitbook.io/dom/ajas/ajax-1/ajax-4.md).

# Ajax 비동기 통신 설정

## 사용하기

동기 통신과 세팅에서 다른 점은 `.open()`에 `true` 값을 전달 한다.&#x20;

```javascript
var xhr = new XMLHttpRequest;
xhr.open('GET', 'ajax/data.txt', true);
xhr.send();

// 서버에서 응답이 올 경우, 이벤트를 감지하여 처리하는 이벤트 핸들링  
xhr.onreadystatechang = function() {
  if((xhr.status === 200 || xhr.status === 304) && xhr.readyState === 4) {
    console.log(xhr, xhr.responseText);
  } else {
    console.warn('통신 실패')
  }
}

```

* `.onreadystatechang` : 서버에서 응답이 올 경우 실행한다.&#x20;

## 이벤트 & 통신상태 확인&#x20;

![](https://1258214208-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQG_6grsIF7SikoY7zP%2F-MRAvZtPx72iL-2Jn-Yo%2F-MRAvbp5E5CUaNtFHziY%2Fimage.png?alt=media\&token=e846da30-894e-4810-b213-ae71ebb7791a)

* 0 : Uninitialized (초기화 되지 않음)
* 1 : Loading
* 2 : Loaded
* 3 : Interactive&#x20;
* 4 : Complete (완료)
