프로젝트 폴더 안에
# flask 시작 코드
from flask import Flask
app = Flask(__name__) # Flask를 객체화 시켜서 app이라는 변수에 저장
# 고유 주소, 부분을 수정해서 URL을 나눌 수 있다,
# url별로 함수명이 같거나 route('/')내의 주소가 같으면 안됨
@app.route('/')
def home():
return 'This is Home!'
if __name__ == '__main__': # 모듈이 실행 됨을 알림
app.run('0.0.0.0',port=5000,debug=True)
# 서버 실행, 파라미터로 debug 여부, port 설정 가능
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
@app.route('/test', methods=['GET']) # url: /test, type: GET
def test_get():
title_receive = request.args.get('title_give') # 'title_give'값 가져오기
print(title_receive)
return jsonify({'result':'success', 'msg': '이 요청은 GET!'})
$.ajax({
type: "GET",
url: "/test?title_give=봄날은간다", # URL뒤에 ?를 붙여 key=value로 전달
data: {},
success: function(response){
console.log(response)
}
})
@app.route('/test', methods=['POST'])
def test_post():
title_receive = request.form['title_give'] # 'title_give'로 가져온 값
print(title_receive)
return jsonify({'result':'success', 'msg': '이 요청은 POST!'})
$.ajax({
type: "POST",
url: "/test",
data: { title_give:'봄날은간다' }, # API코드에 가져온 값 이름이랑 똑같이 해줘야함
success: function(response){
console.log(response)
}
})
# (INTERNAL SERVER ERROR) 서버쪽에서 에러가 났을 때
# app.py의 에러를 확인 해야됨