๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

3-1_์ŠคํŒŒ๋ฅดํƒ€ํ”ผ๋””์•„์— OpenAPI ๋ถ™์—ฌ๋ณด๊ธฐ (feat. ์˜ํ™” ํ‰์  ์นด๋“œ, ajax append(), api ์—ฐ๊ฒฐ) ๋ณธ๋ฌธ

[WEB & AI] (feat. ์ทจ์ค€)/ํ”„๋ก ํŠธ&๋ฐฑ์—”๋“œ

3-1_์ŠคํŒŒ๋ฅดํƒ€ํ”ผ๋””์•„์— OpenAPI ๋ถ™์—ฌ๋ณด๊ธฐ (feat. ์˜ํ™” ํ‰์  ์นด๋“œ, ajax append(), api ์—ฐ๊ฒฐ)

๋ถ€์‹œ๋งค๋‚˜_HA 2023. 1. 29. 16:23
728x90
๋ฐ˜์‘ํ˜•

 

โ… . ์ด๋ฒˆ ํšŒ์ฐจ ํ•™์Šต ๋ชฉํ‘œ (goal)


1. ๋ฐฐ์šฐ๊ณ ์ž ํ•˜๋Š” ๊ฒƒ

  • ๋งŒ๋“ ๊ฒƒ์„ ๋ถ™์—ฌ๋†“๊ธฐ


โ…ก. ํ•ด๋ณด๊ธฐ


1. ๋งŒ๋“ค์–ด์ง„ DB ๋ฅผ API ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์™€์„œ ํ™•์ธ

  • api ์ฃผ์†Œ
http://spartacodingclub.shop/web/api/movie
  • ๋งŒ๋“ค์–ด์ง„ api ํ™•์ธ


2. ๋กœ๋”ฉ ํ›„ ๋ฐ”๋กœ console ์— '๋กœ๋”ฉ์ด ์ž˜ ๋˜์—ˆ๋‹ค.' ๋Š” ๋ฉ”์‹œ์ง€ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ [ajax ๋ผˆ๋Œ€๋งŒ๋“ค๊ธฐ]

$(document).ready(function () {
	listing();
});

function listing() {
	console.log('ํ™”๋ฉด ๋กœ๋”ฉ ํ›„ ์ž˜ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค');
}
  • ์ฝ์–ด๋ณด๊ธฐ

$(document).ready(function () { : ๋กœ๋”ฉ ํ›„ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰์ด ๋ ๊ฑฐ์•ผ. ๊ทธ๊ฒŒ ๋ญ์•ผ?
listing(); : ๋ฆฌ์ŠคํŒ… ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ ๋ ๊ฑฐ์•ผ~! ๊ทธ๋Ÿฌ๋ฉด, ๋ฆฌ์ŠคํŒ… ํ•จ์ˆ˜๋Š” ๋ญ์ง€?
function listing() {console.log('ํ™”๋ฉด ๋กœ๋”ฉ ํ›„ ์ž˜ ์‹คํ–‰๋˜์—ˆ์Šต๋‹ˆ๋‹ค');} : ์ฝ˜์†”์„ ๋„์šด๋‹ค.


  • ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ์ด์œ 
    • ์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์˜ ๋ผˆ๋Œ€๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ  -> ์‹ฌํ™” ๋˜๋Š” ๋ณ€ํ˜• ์‹œ์ผœ๋‚˜๊ฐ€๋Š” ๊ณผ์ •


3. ํ•„์š”ํ•œ data ๊ฐ€ console ์— ์ž˜ ์ฐํžˆ๋Š”์ง€ ํ™•์ธ

  • ์ž˜ ์ฐํžˆ๋Š” ์ฝ”๋“œ
function listing() {
	$.ajax({
		type: "GET",
		url: "http://spartacodingclub.shop/web/api/movie",
		data: {},
		success: function (response) {
			let rows = response['movies']
			
			console.log(rows)
		}
	})
}

  • ๊ฒฐ๊ณผ๋ฌผ - ์ž˜ ๋‚˜์˜ค๊ณ  ์žˆ์Œ!


4. ์ž˜ ๋‚˜์˜ค๋Š” data ๋ฅผ for ๋ฌธ์œผ๋กœ ๋Œ๊ธฐ

  • ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์ž˜ ๋“ค์–ด์˜ค๋Š”์ง€ console ๋กœ ํ™•์ธ
    • ์ด ๊ณผ์ •๋“ค์ด ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ์Œ โญโญโญโญโญ
    • ์ด๊ฒŒ ์ •๋ง ์†์— ์ต์œผ๋ ค๋ฉด, ๋‚ด๊ฐ€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ, ์•ˆ ๋ณด๊ณ  ๋‹ค์Œ ๊ณผ์ •์„ ๋– ์˜ฌ๋ฆฌ๋ฉด์„œ, ๋™์‹œ์— ์ƒ๊ฐ์„ ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Œ โญโญโญโญโญ
function listing() {
	$.ajax({
		type: "GET",
		url: "http://spartacodingclub.shop/web/api/movie",
		data: {},
		success: function (response) {
			let rows = response['movies']
			for (let i = 0; i < rows.length; i++){
				let title = rows[i]['title']
				let desc = rows[i]['desc']
				let image = rows[i]['image']
				let star = rows[i]['star']
				let comment = rows[i]['comment']
				console.log(title, desc, image, star, comment)
			}                    
		}
	})
}
  • ๊ฒฐ๊ณผ๋ฌผ
    • ์ž˜ ๋‚˜์˜ค๊ณ  ์žˆ์Œ.



5. ajax append() ์‚ฌ์šฉํ•ด์„œ, ์นด๋“œ ์ถ”๊ฐ€๋กœ ๋ถ™์ด๊ธฐ

  • ์นด๋“œ ์˜์—ญ html ๋ณต์‚ฌ
    • ์ด ๋†ˆ์„ temp_html ๋ณ€์ˆ˜์— ๋†“๊ณ 
    • ajax append ์— ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.
    • ๊ทธ๋ฆฌ๊ณ , ๊ธฐ์กด ๊ฒƒ๋“ค ์ง€์›Œ์ฃผ๊ธฐ ์œ„ํ•ด์„œ empty() ์‚ฌ์šฉ ํ•˜๋ฉด ๋œ๋‹ค. โญโญโญ
      • ์ด๊ฑธ ํ•ด์•ผ, ์—…๋ฐ์ดํŠธ ๊ธฐ๋Šฅ์ด ์™„์„ฑ ๋  ์ˆ˜ ์žˆ์Œ.
    • ์™€ ์‹ ๊ธฐํ•˜๋‹ค ์ง„์งœ โญโญโญโญโญโญโญโญ
    • ๊ทธ๋ฆฌ๊ณ  append ์“ธ ๋•Œ, html ์„ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•  ํ•„์š” ์—†๊ณ , ๊ทธ๋ƒฅ body ๋ถ€๋ถ„์— ์žˆ๋Š” ๊ฒƒ๋“ค ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์•ผ ํ•˜๋ ค๋‚˜.
<div class="col">
	<div class="card">
		<!-- ํฌ์Šคํ„ฐ ์ด๋ฏธ์ง€ ๋„ฃ์–ด์ฃผ๊ธฐ โญโญโญ  -->
		<img src="https://movie-phinf.pstatic.net/20210728_221/1627440327667GyoYj_JPEG/movie_image.jpg"
			class="card-img-top" alt="...">
		<div class="card-body">
			<h5 class="card-title">์—ฌ๊ธฐ์— ์ œ๋ชฉ์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค! </h5>
			<p class="card-text">์—ฌ๊ธฐ์—๋Š” ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ€์ง€์š”. ํ“จ์Š. ์œจ๋•์ด๊ฐ€ ์ปจ๋””์…˜์ด ์•ˆ ์ข‹์ง€๋งŒ ํ•ด๋ƒ„๊ฑฐ์ž„ ๐Ÿ’‍โ™€๏ธ </p>

			<!-- ๋ณ„ํ‘œ ์˜์—ญ -->
			<p> โญโญโญ </p>

			<!-- ์ฝ”๋ฉ˜ํŠธ ์˜์—ญ -->
			<p class="mycomment"> ์—ฌ๊ธฐ์— ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. </p>

		</div>
	</div>
</div>

  • ์šฐ์„  ์นด๋“œ ์ถ”๊ฐ€ ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ
function listing() {
	// ๊ธฐ์กด์— ์žˆ๋Š” ๊ฒƒ๋“ค ์ง€์›Œ์•ผ, ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ.  
	$('#cards-box').empty()

	$.ajax({
		type: "GET",
		url: "http://spartacodingclub.shop/web/api/movie",
		data: {},
		success: function (response) {
			let rows = response['movies']
			for (let i = 0; i < rows.length; i++) {
				let title = rows[i]['title']
				let desc = rows[i]['desc']
				let image = rows[i]['image']
				let star = rows[i]['star']
				let comment = rows[i]['comment']

				let temp_html = `<div class="col">
									<div class="card">
										<!-- ํฌ์Šคํ„ฐ ์ด๋ฏธ์ง€ ๋„ฃ์–ด์ฃผ๊ธฐ โญโญโญ  -->
										<img src="${image}"
											class="card-img-top" alt="...">
										<div class="card-body">
											<h5 class="card-title">${title}</h5>
											<p class="card-text">${desc}๐Ÿ’‍โ™€๏ธ</p>

											<!-- ๋ณ„ํ‘œ ์˜์—ญ -->
											<p>โญโญโญ </p>

											<!-- ์ฝ”๋ฉ˜ํŠธ ์˜์—ญ -->
											<p class="mycomment">${comment}</p>

										</div>
									</div>
								</div>`
				
				$('#cards-box').append(temp_html)

			}
		}
	})
}

  • ๊ฒฐ๊ณผ๋ฌผ - ์ž˜ ์ž‘๋™ํ•จ


6. ๋ณ„์  : javascript ๋‚ด์žฅ ํ•จ์ˆ˜ ์‚ฌ์šฉ

  • repeat ํ•จ์ˆ˜
    • ์ง€์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ, ํŠน์ • ๋ถ€๋ถ„์„ ๋ฐ˜๋ณต
// ๋ณ„ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ star ๋ณ€์ˆ˜์— ๋‹ด๊ธด ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต!
let star_image = 'โญ'.repeat(star)


๐Ÿ’ญ What i learned


  • ajax ๋ฅผ ๋ถ™์—ฌ๋„ ๊ดœ์ฐฎ์€ ๋ผˆ๋Œ€๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ , ๊ทธ ๋‹ค์Œ ajax ๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ! ex) ๋กœ๋”ฉ ๋˜์—ˆ์„ ๋•Œ, console ์ฐฝ์— '๋กœ๋”ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.' ๋ผ๊ณ  ๋ณด์ด๊ฒŒ ํ•˜๋ฉด ๋œ๋‹ค. -> ๊ทธ ๋‹ค์Œ ajax ๋ฅผ ๋ถ™์ธ๋‹ค.
    • ์‰ฝ๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด, console.log ์ž๋ฆฌ์— ajax ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๊ณ  ์ƒ๊ฐ.

  • ๊ทธ๋ฆฌ๊ณ  append ์“ธ ๋•Œ, html ์„ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•  ํ•„์š” ์—†๊ณ , ๊ทธ๋ƒฅ body ๋ถ€๋ถ„์— ์žˆ๋Š” ๊ฒƒ๋“ค ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ด์•ผ ํ•˜๋ ค๋‚˜.
728x90
๋ฐ˜์‘ํ˜•
Comments