Mainpage

server와의 통신

사용자가 입력한 lms 아이디와 비밀번호를 서버에 넘기고 json의 형태로 응답을 받는다.

미리 작성해둔 API에 따라 method는 post 방식으로, 사용자의 LMS ID와 PW를 서버에 request를 보낸다.

function loginmove() {
    fetch("/login", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                pk_user_id: document.getElementById("username").value,
                pk_user_pw: document.getElementById("password").value,
            }),
        }).then((response => response.json()))
        .then(json => {
            if (json['status'] == 400) {
                 비밀번호가 틀렸을 때
             }else if (json['status'] == 500) {
                 서버 에러가 발생했을 때
             }else if (json['status'] == 200) {
                 사용자의 lms 데이터를 받아왔을 때
             }
 }

이때 json의 형태로 id와 password 값을 전송하기 때문에 Content-Type 헤더를 application/json로 설정했고 body에서는 JSON.stringify을 이용해서 JSON 문자열로 변환을 해서 request를 보내야 한다.

json 문자열로 응답받은 response는 js 객체로 변환하고, json의 'status’값에 따라 사용자에게 오류를 알리던지, secondpage로 넘어가 미제출 항목을 보여주게 된다.

Last updated