4주차_Flask_get, post(3)

4주차_Flask_get, post(3)

우리가 만들 API 두 가지

1) 포스팅API - 카드 생성 (Create) : 클라이언트에서 받은 url, comment를 이용해서 정보를 찾고 저장하기 (POST)

2) 리스팅API - 저장된 카드 보여주기 (Read) (GET)

2)GET

[서버 코드 - app.py]

@app.route('/memo', methods=['GET'])

def listing():

articles = list(db.articles.find({},{'_id': False}))

return jsonify({'all_articles' : articles})

메모를 보여주기 위해 서버가 추가로 전달받아야하는 정보는 없습니다.

따라서 서버 로직은 다음 단계로 구성되어야 합니다.

1. mongoDB에서 _id 값을 제외한 모든 데이터 조회해오기 (Read)

2. articles라는 키 값으로 articles(list(db.articles.find({},{'_id': False}) 이 부분) 정보 보내주기

[클라이언트 코드 - index.html]

function showArticles() {

$.ajax({

type: "GET",

url: "/memo",

data: {},

success: function (response) {

let articles = response['all_articles']

for (let i =0; i

let title = articles[i]['title']

let image = articles[i]['image']

let url = articles[i]['url']

let desc = articles[i]['desc']

let comment = articles[i]['comment']

let temp_html = `

src="${image}"

alt="Card image cap">

${title}

${desc}

${comment}

`

$('#cards-box').append(temp_html)

}

}

})

}

메모를 작성하기 위해 서버에게 주어야하는 정보는 없습니다. 조건없이 모든 메모를 가져오기 때문입니다.

따라서 클라이언트 로직은 다음 단계로 구성되어야 합니다.

1. /memo에 GET 방식으로 메모 정보 요청하고

articles로 메모 정보 받기

2.함수를 이용해서 카드 HTML 붙이기

from http://ojy4535.tistory.com/40 by ccl(A) rewrite - 2021-07-24 23:00:13