카테고리 없음

4주차 (2)

Yoon_Med 2021. 12. 29. 11:26

POST 요청 API코드 

 

앞으로는 post에서 많이 데이터 가져간다. 

 

<script>
    function hey() {
        $.ajax({
            type: "POST",
            url: "/test",
            data: {title_give: '봄날은간다'},
            success: function (response) {
                console.log(response)
            }
        })
    }
</script>

--> 1) title_give로 '봄날은간다' 입력 

 


@app.route('/test', methods=['GET'])
def test_get():
   title_receive = request.args.get('title_give')
   print(title_receive)
   return jsonify({'result': 'success', 'msg': '이 요청은 GET!'})

@app.route('/test', methods=['POST'])
def test_post():
    title_receive = request.form['title_give']
    print(title_receive)
    return jsonify({'result':'success', 'msg': '이 요청은 POST!'})

/test라는 창구를 만들었고, POST로 데이터를 받으면 이쪽(아래쪽)으로 온다.  : GET은 위로 가겠지. 

2) title_give를 받아 와서 title_receive로 print에 찍어준다.   --> 이건 python에 입력된다. 

3) 이후 return jsonify로 돌려주는데, msg를 돌려준다. 

<script>
    function hey() {
        $.ajax({
            type: "POST",
            url: "/test",
            data: {title_give: '봄날은간다'},
            success: function (response) {
                console.log(response)
            }
        })
    }
</script>

4)그러면 이 response : msg를 console.log에 입력한다. --> console 창에 '이 요청은 GET!'이 뜨게 된다. 

 

 

**jsonify --> 'result':'success' -- key : value // 'msg':'이 요청은 GET!' -- key:value 

그래서 만약 여기서 response['msg']만 찍어준다면?! 

 

<script>
    function hey() {
        $.ajax({
            type: "POST",
            url: "/test",
            data: {title_give: '봄날은간다'},
            success: function (response) {
                console.log(response['msg'])
            }
        })
    }
</script>

이렇게 msg 내용만 value로 나오게 된다!!! 

 

원래는 

이렇게 나온다!!