Ajax?
XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200 || xhr.status === 201) {
console.log(xhr.responseText);
} else {
console.error(xhr.responseText);
}
}
};
Axios?
Axios 설치방법
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
npm install axios
.then()
.catch()
GET방식
async function getUser() {
try {
const response = await axios.get('/replies/list/${bno}');
console.log(response);
} catch (error) {
console.error(error);
}
}
POST방식
async function sendPost(reply){
const res = await axios.post('/replies/',reply)
console.log(res)
}
####
1. 기본적으로 Json 방식으로 처리, Fetch API는 기본적으로 Json 방식이 아님
2. 비동기처리는 Promise로 리턴
3. async - await는 비동기식 처리에서 동기식처럼 코드를 처리할 수 있게 도와주는 것