4주차 요약
서버에 데이터 저장 -> 불러오기
1시간 고민한 문제의 원인은 {}
flask 프레임 워크 - 서버를 구동시켜주는 편한 코드모음.
from flask import Flask #젤 처음해야함
app = Flask(__name__)
@app.route('/')
def home():
return 'This is Home!'
@app.route('/mypage')
def mypage():
return 'This is My Page!'
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)
#app.run에서 host 설정은 모든 IP에 대한 접근은 허용한다는 의미
Flask 서버를 만들 때 항상
프로젝트 폴더 안에
ㄴstatic 폴더 (이미지, css파일을 넣어둡니다)
ㄴtemplates 폴더 (html파일을 넣어둡니다)
ㄴapp.py 파일(서버)
(새로운 파이참 프로젝트 생성시 flask, pymongo를 install하고 시작)
GET 요청 api 코드
@app.route('/test', methods=['GET'])
def test_get():
title_receive = request.args.get('title_give')
print(title_receive)
return jsonify({'result':'success', 'msg': '요청완료!'})
GET 요청 확인 Ajax코드
$.ajax({
type: "GET",
url: "/test?title_give=가아아앖",
data: {},
success: function(response){
console.log(response)
}
})
POST 요청 API코드
@app.route('/test', methods=['POST'])
def test_post():
title_receive = request.form['title_give']
print(title_receive)
return jsonify({'result':'success', 'msg': '요청완료!'})
POST 요청 확인 Ajax코드
$.ajax({
type: "POST",
url: "/test",
data: { title_give:'값' },
success: function(response){
console.log(response)
}
})
(console.log)
Lexical declaration cannot appear in a single-statement context
라는 에러가 발생했다
솔직히 구글링해도 원인이 무엇인지 해결방법은 무엇인지 잘모르겠다..
그러다 코드 재작성하면서 알게된건 function(){}에서 {}를 쓰지않아 발생했던 에러 같다.
허구한날 오타내서 막힐때 짜증이 나지만 우연치않게 해결했을때는 좋다.
물론 이게 맞게 해결했는지는 조금더 지나봐야알겠지만