커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
4주차 숙제중입니다.
undefined주차
북마크
송*환
댓글
3
추천
0
조회수
10
조회수
10
답변 완료

가수 팬명록을 만들어서 서버와 연결시킨 후

닉네임과 응원을 남겨 버튼을 누르면 서버에 해당 데이터가 전송되고

그 내용이 홈페이지에 차례대로 뜨는 숙제를 하고 있는데


버튼을 눌러 서버로 보내는 것은 잘 작동이 되지만

서버에서 데이터를 가져와 홈페이지에 차례로 띄우는 부분에서 작동이 되지 않습니다.


@app.route('/')
def home():
   return render_template('index.html')

@app.route("/homework", methods=["POST"])
def homework_post():
    name_receive = request.form['name_give']
    comment_receive = request.form['comment_give']
    doc = {
        'name': name_receive,
        'comment' : comment_receive
    }
    db.homework.insert_one(doc)
    return jsonify({'msg':'저장 완료!'})

@app.route("/homework", methods=["GET"])
def homework_get():
    comment_list = list(db.homework.find({},{'_id':False}))
    return jsonify({'comments':comment_list})

if __name__ == '__main__':
   app.run('0.0.0.0', port=5000, debug=True)


app.py 의 해당 부분이고


    <script>
        $(document).ready(function(){
            set_temp()
            show_comment()
        });


        function save_comment() {
            let name = $('#name').val()
            let comment = $('#comment').val()

            $.ajax({
                type: 'POST',
                url: '/homework',
                data: {'name_give': name, 'comment_give':comment},
                success: function (response) {
                    alert(response['msg'])
                    window.location.reload()
                }
            });
        }
        function show_comment() {
            $('#comment-list').empty()
            $.ajax({
                type: "GET",
                url: "/homework",
                data: {},
                success: function (response) {
                    console.log(response['comments'])
                    let rows = response['comments']
                    for (let i = 0; i < rows.length; i++) {
                        let name = rows[i]['name']
                        let comment = rows[i]['comment']

                        let temp_html = `<div class="card">
                                            <div class="card-body">
                                                <blockquote class="blockquote mb-0">
                                                    <p>${comment}</p>
                                                    <footer class="blockquote-footer">${name}</footer>
                                                </blockquote>
                                            </div>
                                        </div>`
                        $('#comment-list').append(temp_html)
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div class="mypic">
        <h1>리쌍 팬명록</h1>
        <p>현재기온: <span id="temp">36</span></p>
    </div>
    <div class="mypost">
        <div class="form-floating mb-3">
            <input type="text" class="form-control" id="name" placeholder="url">
            <label for="floatingInput">닉네임</label>
        </div>
        <div class="form-floating">
            <textarea class="form-control" placeholder="Leave a comment here" id="comment"
                style="height: 100px"></textarea>
            <label for="floatingTextarea2">응원댓글</label>
        </div>
        <button onclick="save_comment()" type="button" class="btn btn-dark">응원 남기기</button>
    </div>
    <div class="mycards" id="comment-list">

    </div>
</body>
</html>


index.html입니다.


문제를 파악해보려

index.html의 get방식 ajex 부분에 console.log(response['comments'])를 사용해서
 홈페이지에 콘솔창을 띄워서 확인해보려 했으나, 콘솔창에는 이렇게 메시지가 뜹니다.

스파르타 즉문즉답


취소
 공유
취소
댓글 0
댓글 알림
나의얼굴