Python

[파이썬] Flask - GET 방식에서 Ajax 파라미터 사용하는법

Sungwoo Koo 2022. 4. 20. 17:05

GET 방식도 POST와 똑같이 request.form 으로 데이터 받아올거라 생각했다가 KeyError에 시달렸다.

같은 문제로 구글 여행을 떠나지 않기 위해 기록한다.

 

@app.route('/profile', methods=['GET'])
def view_profile():
    name_receive = request.args.get('name')
   ...
    return jsonify({
                    'user_name': name_receive
                    })

 

request.args.get() 메서드를 사용하면

 

$.ajax({
    type: "GET",
    url: "/profile",
    data: {name:name},
    success: function (response) {
    ...
    }

 

GET 방식 ajax에서 보낸 데이터를 받을 수 있다.