웹 프로그래밍 4주차

kinghong97·2021년 12월 16일
0

웹 프로그래밍

목록 보기
4/5

flask

프레임 워크

서버를 구동하기에 편함

html 불러오기

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

url 나누기
@app.route('/') 에서
def hi():
return render_template('hello.html')
/다음에 들어가는게 다르게 한다

html 파일을 담는 templates 폴더와 css img js 파일을 담는 static 폴더를 생성한다

api 만들기

클라이언트에서 서버에 요청하는 ajax 코드를

서버에서 들어온 요청에 응답하는 코드를 작성한다

get 예시

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

클라이언트
$.ajax({
type: "GET",
url: "/test?title_give=봄날은간다",
data: {},
success: function(response){
console.log(response)
}
})

post 예시

서버

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

클라이언트

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

0개의 댓글