서버에 Request시 오류일 때 위 글 참고
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <!-- axios 추가 -->
</head>
<h1>hi</h1>
<body>
// 버튼 누르면 POST 요청 감
<button id="btn" onclick="join();"> 제출합니다</button>
<script>
const dataContainer = document.getElementById('data-container'); // 결과를 표시할 HTML 요소 가져오기
function join() {
// API 엔드포인트 URL
const options = {
method: 'POST',
url: 'https://eodikase.com/v1/members',
data: {
"password": "123abc***",
"email": "email@naver.com",
"nickname": "홍길동"
},
//withCredentials: false,
// params: {
// access_token: SECRET_TOKEN,
// },
// mode: 'no-cors',
headers: {
// 'Access-Control-Allow-Origin': '*', // 서버가 CORS 처리 해줘야함
'Content-Type': 'application/json', // 지우면 400 에러 뜨게됨
},
};
// POST 요청 보내기
axios.request(options)
.then(response => {
// 응답 데이터 처리
console.log('POST Response Data:', response.data);
dataContainer.innerText = '결과: ' + JSON.stringify(response.data, null, 2);
})
.catch(error => {
// 오류 처리
console.error('POST Error:', error);
});
}
</script>