REST(HTTP를 기반으로 클라이언트가 서버의 리소스에 접근하는 방식을 규정한 아키텍처)를 기반으로 서비스 api를 구현한 것
구성 : URI, HTTP 요청 메서드, 페이로드(전송되는 테이터)
const xhr = new XMLHttpRequset();
xhr.open('GET', '/todos');
xhr.send();
xhr.onload = () => {
if(xhr.status === 200){
console.log(JSON.parse(xhr.response));
// {"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}
}else{
console.error('Error', xhr.status, xhr.statusText);
}
};
const xhr = new XMLHttpRequset();
xhr.open('POST', '/todos');
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify({id : 4, content : 'Angular', completed : false});
xhr.onload = () => {
if(xhr.status === 200 || xhr.status === 201){ // OK || Created
console.log(JSON.parse(xhr.response));
}else{
console.error('Error', xhr.status, xhr.statusText);
}
};
const xhr = new XMLHttpRequset();
xhr.open('PUT', '/todos/4');
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify({id : 4, content : 'React', completed : true});
xhr.onload = () => {
if(xhr.status === 200){
console.log(JSON.parse(xhr.response));
}else{
console.error('Error', xhr.status, xhr.statusText);
}
};
const xhr = new XMLHttpRequset();
xhr.open('PATCH', '/todos/4');
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify({completed : false});
xhr.onload = () => {
if(xhr.status === 200){
console.log(JSON.parse(xhr.response));
}else{
console.error('Error', xhr.status, xhr.statusText);
}
};
const xhr = new XMLHttpRequset();
xhr.open('DELTE', '/todos/4');
xhr.send(JSON.stringify({completed : false});
xhr.onload = () => {
if(xhr.status === 200){
console.log(JSON.parse(xhr.response));
}else{
console.error('Error', xhr.status, xhr.statusText);
}
};