mylogin.js

팡태(❁´◡`❁)·2021년 12월 14일
0

html/css/javascript

목록 보기
19/20

// getElementById는 위쪽 태그중에서 id가 btn1인 태그를 찾음
const b1 = document.getElementById('btn1');

b1.addEventListener('click', function() {
    const id = document.getElementById('userid');
    const pw = document.getElementById('userpw');

    if (id.value === "") {
        alert('아이디를 입력하세요.');
        id.focus();
        return false; //벡엔드로 전송 하지 않음
    }

    if ( pw.value === "" ){
        alert('암호를 입력하세요.');
        pw.focus();
        return false; //벡엔드로 전송 하지 않음
    }

    const url = `http://ihongss.com/json/login.json?userid=${id.value}&userpw=${pw.value}`;
    let xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.send();

    xhr.addEventListener('load', function(){
        const data = JSON.parse(xhr.response);
        console.log(data); //{ret:'y'} or {ret:'n'}
        if(data.ret === 'y'){
            alert('로그인 되었습니다.');
            window.location.href = "main.html"; //<a href="main.html">
        }
        else if(data.ret === 'n'){
            alert('아이디 또는 암호를 확인하세요.');
        }
    });
});

0개의 댓글