자바스크립트 함수에서 post 방식으로 값 전달하기

조회수 4511회
function fn_edit_username(method, path) {

        method = method || "post";
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("action", path);

        swal({   title: "An input!",
                  text: "Write something interesting:",
                  type: "input",
                  showCancelButton: true,
                  closeOnConfirm: false,
                  animation: "slide-from-top",
                  inputPlaceholder: "Write something"
          }, function(inputValue) {
                if (inputValue === false)
                  return false;

                if (inputValue === "") {
                  swal.showInputError("You need to write something!");
                  return false;
                }

                swal("Nice!", "You wrote: " + inputValue, "success");
                document.body.appendChild(form);
                form.submit();

            });


      }

node.js 웹앱입니다. 팝업창에서 input 데이터를 입력 받고, 이 값을 mysql 에 저장하려고 합니다.

그런데 멘붕 됐습니다...

값 전달을 어찌해야하나요?

post ? 로 하면 될 것 같아서 찾아보니 위 코드 같이 시도하려 했습니다.

app.post('/edit_username', function(req, res) {
  pool.getConnection(function(err, conn) {
      console.log(req.body.editname);

    });
});

그런데 app.js 에서 값을 어떻게 받아야 할지 모르겠네요. 위와 같이 받으려 했더니 값이 없고...

좋은 방법이나 참고 문서가 있으면 부탁드립니다.

1 답변

  • express를 사용중이신 걸로 보이는데 bodyParser가 추가되지 않은것 같습니다.

    이 부분이 express 버전에 따라 사용법이 많이 달라지기 때문에 맞는 버전에 맞는 문서 확인이 필요합니다.

    Express Document

    아래와 비슷한 부분이 추가되었는지 확인해 보세요.

    // init modules
    app.use(express.bodyParser());
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)