sitelink1 | |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
extra_vars4 | |
extra_vars5 | |
extra_vars6 |
아래의 예제들은 스크립트상에서는 오류도 없고 요청도 잘 보낸다.
하지만 톰캣의 기본 설치 상태에서는 전송한 데이터를 받을 수가 없었다. (HttpServletRequest 객체에 값이 없다)
서버 설정을 다양하게 바꿔봐도 문제를 해결하지 못해서 그래서 결국 fetch 대신 XMLHttpRequest 을 여전히 이용중이다.
다만 fetch 는 최신 스펙이므로 추후 본격적으로 활용할때를 대비해서 예제를 저장해둔다.
자세한게 필요하면 copilot 에게 물어보면 친절히 알려준다.
var data = {
'cmd' : 'getMallList'
};
fetch('/jbs/jbg', {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
// 응답 객체가 성공적으로 돌아왔는지 확인합니다.
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
// 응답을 JSON으로 변환합니다.
return response.json();
})
.then(data => {
if (data.code == '000') {
}
})
.catch(error => console.error('Error:', error));
json 형태로 데이터를 서버에 전송하는 예제다.
promise 를 써서 응답데이터 처리도 하고 있다.
var data = {
'cmd' : 'fetchOrders',
'seqm' : mall.seq,
'id' : mallId,
'pw' : mallPw
};
fetch('/jbs/jbg', {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).catch(error => console.error('Error:', error));
데이터 전송후 응답을 기다리지 않는다. (후처리가 필요없는 경우 유용)
let formData = new FormData();
formData.append('cmd', 'fetchOrders');
formData.append('seqm', mall.seq);
formData.append('id', mallId);
formData.append('pw', mallPw);
fetch('/jbs/jbg', {
method: 'POST',
body: formData
});
이 예제 역시 후처리가 필요없어서 서버에 요청하고 에러 처리도 없이 끝내고 있다.
다만 formData 형태로 서버에 데이터를 전송하고 싶을때 참고한다.