inflearn logo
강의

講義

知識共有

Node.js Web開発で学ぶバックエンドJavaScriptの理解

MySQL連携の実装

c오류 같은데,, 실행이 안돼요

314

sowhat0516

投稿した質問数 1

0

코드를 동일하게 작성한 것 같은데 자꾸 오류가 나네요.. response에도 뜨지 않고 ajaxsend 버튼을 눌러도 밑에 result가 뜨지 않아요

 

그래서 form.html에서 xhr 정보를 console.log로 찍어봤는데요

다 비어있어요... 어쩌면 좋을까요?

network에는 pending인 상태로 뜨네요

 

전체 코드는 아래에 첨부합니다

 

<form.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>email form</title>
  </head>
  <body>
    <form action="/email_post" method="post">
      email : <input type="text" name="email"><br/>
      <input type="submit">
    </form>

    <button class="ajaxsend">ajaxsend</button>

    <div class="result"></div>

    <script>
      document.querySelector('.ajaxsend').addEventListener('click', function () {
        var inputdata = document.forms[0].elements[0].value;
        sendAjax('http://localhost:3000/ajax_send_email', inputdata);
      })

      function sendAjax(url, data){
        var data = {'email' : data};
        data = JSON.stringify(data); // 문자열로 반환
        console.log(data);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', "application/json");
        xhr.send(data);
       
        console.log(xhr.response);
        console.log(xhr.responseText);
        console.log(xhr.responseType);
        console.log(xhr.responseURL);
        console.log(xhr.responseXML);
        console.log('readystate : ' + xhr.readyState);
        console.log('status : ' + xhr.status);
        console.log('statusText : ' + xhr.statusText);

       
        xhr.addEventListener('load', function() {
          console.log(xhr.responseText);
          var result=JSON.parse(xhr.responseText); // 문자열이니까 JSON.parse
          var resultDiv = document.querySelector(".result");

          if(result.result !=="ok") resultDiv.innerHTML="your email is not found";
          else resultDiv.innerHTML=result.name;
        })
      }
    </script>
  </body>
</html>

 

<app.js>

app.post('/ajax_send_email', function(req, res) {
  var email = req.body.email;
  var responseData = {};

  var query = connection.query(
    'select name from user where email="'+ email + '"',
    function (err, rows) {
    if(err) throw err;
    if(rows[0]){ // 첫번째 data 있으면
      responseData.result = "ok";
      responseData.name = rows[0].name;
    }
    else{    
      responseData.result = "none";
      responseData.name = "";
    }
    res.json(responseData)
  })
})

 

nodejs javascript express

回答 1

0

sowhat0516

참고로 jsman에 데이터는 있는 상황입니다. . payload도 출력돼요

Error: Failed to deserialize user out of session

0

360

0

넌적스 for 문 질문

0

199

0

deserializeUser가 호출이 되지 않습니다. undefined문제

1

641

2

Users 테이블을 만드는 sql 명령어를 좀

0

282

1

점점 사운드가 낮아지네여 ㅋㅋㅋㅋㅋ

0

164

0

쿼리 날릴때 불필요한게 있네요

0

185

0

바디 파서로 값이 들어올때

0

211

1

npm nodemon 관련 에러

0

243

0

어.... 소스코드를 어디서 찾을수 있을까요?

0

202

0

consloe.log 가 찍히지 않음

0

189

0

form.html:1 Access to XMLHttpRequest at 요런 요류

3

647

2

ejs 사용할 때 에러 나시는 분들 팁

0

703

0

과제 답안

0

197

0

모듈을 require하는 기준이 궁금합니다.

0

219

0

1분20초 부분에서 submit을 눌러도 반응이 없는데 어떤문제일까요???

0

412

1

git 올린 db자료

0

174

0

ejs를 사용하지 않아도 괜찮은가요?

0

478

0

약간 신기한 문제가 발생했습니다.

0

333

2

계속 undefind 가 뜹니다.

0

316

1

mysql 설치및 설정방법

4

217

0

form 에서 보낸게 서버측에서 읽지 못합니다..

1

281

2

Cannot read property 'email' of undefined

2

504

3

movie/list 화면의 css소스 부탁드립니다.

0

157

0

mysql연동 에러입니다.

0

498

2