HTTP request를 만들고 onreadystatechange 속성을 설정하여 응답을 처리할 JavaScript 함수를 XMLHttpRequest 객체에 알려줘야 한다
const httpRequest = new XMLHttpRequest();
function handler() {
// Process the server response here.
}
httpRequest.onreadystatechange = handler;
httpRequest.onreadystatechange = () => {
// Process the server response here.
};
httpRequest.open("GET", "http://www.example.org/some.file", true);
httpRequest.send();
httpRequest.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded"
);
서버 응답을 처리한다
httpRequest.onreadystatechange = nameOfTheFunction;
if (httpRequest.readyState === XMLHttpRequest.DONE) {
// Everything is good, the response was received.
} else {
// Not ready yet.
}
if (httpRequest.status === 200) {
// Perfect!
} else {
// There was a problem with the request.
// For example, the response may have a 404 (Not Found)
// or 500 (Internal Server Error) response code.
}
XML response, data를 다룬다
참고