POST 소스코드

JeongInHuh·2023년 9월 22일
0

백엔드와 협업 - 서버 통신하기

서버에 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>
profile
컴퓨터공학부 대학생. 4학년. velog 꾸미기 : https://velog.io/@ybkim3603/Velog벨로그-사용법-튜토리얼 Git컨벤션: https://velog.io/@shin6403/Git-git-커밋-컨벤션-설정하기 커리어 방향 설정 모음글:https://velog.io/@eon7500/커리어-방향성-설정에-도움되는-글

0개의 댓글