GET, POST ๋ฐฉ์‹

Seomneยท2022๋…„ 8์›” 27์ผ
0

๐Ÿ‘‰ GET โ†’ ํ†ต์ƒ์ ์œผ๋กœ! ๋ฐ์ดํ„ฐ ์กฐํšŒ(Read)๋ฅผ ์š”์ฒญํ•  ๋•Œ
์˜ˆ) ์˜ํ™” ๋ชฉ๋ก ์กฐํšŒ
โ†’ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ : URL ๋’ค์— ๋ฌผ์Œํ‘œ๋ฅผ ๋ถ™์—ฌ key=value๋กœ ์ „๋‹ฌ
โ†’ ์˜ˆ: google.com?q=๋ถ๊ทน๊ณฐ
1. ์š”์ฒญ ์ •๋ณด : URL= /mars, ์š”์ฒญ ๋ฐฉ์‹ = GET
2. ํด๋ผ(ajax) โ†’ ์„œ๋ฒ„(flask) : (์—†์Œ)
3. ์„œ๋ฒ„(flask) โ†’ ํด๋ผ(ajax) : ์ „์ฒด ์ฃผ๋ฌธ์„ ๋ณด๋‚ด์ฃผ๊ธฐ

๐Ÿ‘‰ POST โ†’ ํ†ต์ƒ์ ์œผ๋กœ! ๋ฐ์ดํ„ฐ ์ƒ์„ฑ(Create), ๋ณ€๊ฒฝ(Update), ์‚ญ์ œ(Delete) ์š”์ฒญ ํ•  ๋•Œ
์˜ˆ) ํšŒ์›๊ฐ€์ž…, ํšŒ์›ํƒˆํ‡ด, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ˆ˜์ •
โ†’ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ : ๋ฐ”๋กœ ๋ณด์ด์ง€ ์•Š๋Š” HTML body์— key:value ํ˜•ํƒœ๋กœ ์ „๋‹ฌ
1. ์š”์ฒญ ์ •๋ณด : URL= /mars, ์š”์ฒญ ๋ฐฉ์‹ = POST
2. ํด๋ผ(ajax) โ†’ ์„œ๋ฒ„(flask) : name, address, size
3. ์„œ๋ฒ„(flask) โ†’ ํด๋ผ(ajax) : ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ„ (์ฃผ๋ฌธ ์™„๋ฃŒ!)

  • Jquery ์ž„ํฌํŠธ
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  • 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!'})
  • 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!'})
  • POST ์š”์ฒญ ํ™•์ธ Ajax์ฝ”๋“œ
$.ajax({
    type: "POST",
    url: "/test",
    data: { title_give:'๋ด„๋‚ ์€๊ฐ„๋‹ค' },
    success: function(response){
       console.log(response)
    }
  })
profile
์ƒ์ƒํ•˜๋ฉฐ ์„ฑ์žฅํ•˜๋Š” ํผ๋ธ”๋ฆฌ์…”

0๊ฐœ์˜ ๋Œ“๊ธ€