๋ฐ˜์‘ํ˜•
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 ๐Ÿ™Œ

2-7_Ajax ์‹œ์ž‘ํ•˜๊ธฐ ๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

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


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

  • ๋„ค์ด๋ฒ„์—์„œ enter
    ์น˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ==javascript ์—์„œ ์–ด๋–ป๊ฒŒ ์„œ๋ฒ„์— ์š”์ฒญ ํ•˜๋Š”์ง€!== ์•Œ์•„๋ณด๊ธฐ


โ…ก. (๋ณต์Šต) JSON ํŒŒ์ผ ํ˜•์‹ ๋ง›๋ณด๊ธฐ


1. ์ด๋ ‡๊ฒŒ list, dictionary ํ•ฉ์ณ์ ธ์„œ ๋‚ด๋ ค์˜ค๋Š” ํ˜•์‹ = JSON ํŒŒ์ผ ํ˜•์‹



โ…ก. Ajax ๊ธฐ๋ณธ


1. ๋Œ€์ „์ œ

  • ==jquery ๊ฐ€ ์ด๋ฏธ ์„ค์น˜== ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ

2. ajax ๊ธฐ๋ณธ๊ณจ๊ฒฉ


a. ์ž‘์„ฑ์œ„์น˜
  • javascript ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฑฐ๋‹ˆ๊นŒ, ==script ํƒœ๊ทธ== ์— ์ž‘์„ฑ

b. ajax ๊ธฐ๋ณธ๊ณจ๊ฒฉ ์ฝ”๋“œ #ajax_๊ธฐ๋ณธ๊ณจ๊ฒฉ_์˜ˆ์ œ์ฝ”๋“œ
$.ajax({
    type: "GET",
    url: "์—ฌ๊ธฐ์—URL์„์ž…๋ ฅ",
    data: {},
    success: function (response) {
        console.log(response)
    }
})
  • [์ฝ์–ด๋ณด๊ธฐ] ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์–ด๋–ค ๊ฒฐ๊ณผ๋ฌผ์ด ๋‚˜์™€?

    get ๋ฐฉ์‹์œผ๋กœ ํ•ด๋‹น url ์— ์š”์ฒญํ•ด์„œ reponse ๋ฅผ ๋ฐ›์•„์˜ค๊ณ  -> ๋ฐ›์•„์˜จ response๋ฅผ console ์— ์ฐ์–ด์ฃผ๊ฒ ๋‹ค.

  • ์ž…๋ ฅํ•˜๋Š” url
  • ๊ฒฐ๊ณผ๋ฌผ
    ๐Ÿ‘‰ ์˜ค, ๋ญ”๊ฐ€ ๋ฐ›์•„์ง„๊ฑฐ ๊ฐ™์Œ!!!


3) ๋ฐ›์•„์˜จ response ์—์„œ ์ข€ ๋” ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ


a. ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์„ ์กด์ค‘ํ•ด์„œ ๊ธฐ์žฌํ•˜๊ณ  -> ํ•œ๋ฒˆ console ๋กœ ์™”๋Š”์ง€ ํ™•์ธ โญโญโญโญโญ
$.ajax({
    type: "GET",
    url: "http://spartacodingclub.shop/sparta_api/seoulair",
    data: {},
    success: function (response) {
        console.log(response['RealtimeCityAir'])
    }
})
  • ์ฝ์–ด๋ณด๊ธฐ

    console.log(response['RealtimeCityAir']) : ์ด๋ ‡๊ฒŒ ๊ธฐ์žฌํ•œ ๊ฒƒ์€ 'dict' ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— key ๊ฐ’์„ ๋„ฃ์–ด์„œ value ๋ฅผ ๊ฐ€์ ธ์˜จ๊ฑฐ์•ผ! โญโญโญ



4) ์€ํ‰๊ตฌ 30 ์ด๋ ‡๊ฒŒ ์ฐ์–ด๋ณด๊ธฐ


์šฐ์„  ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ
$.ajax({
    type: "GET",
    url: "http://spartacodingclub.shop/sparta_api/seoulair",
    data: {},
    success: function (response) {

        let rows = response['RealtimeCityAir']['row']

        for (let i = 0; i < rows.length; i++) { 
            let gu_name = rows[i]['MSRSTE_NM']
            let gu_mise = rows[i]['IDEX_MVL']
            console.log(gu_mise, gu_name)
        }
    }
})

  • ์ฝ์–ด๋ณด๊ธฐ

1 block : ๋ฐ์ดํ„ฐ๋ฅผ READ ํ•˜๊ณ  ์‹ถ์–ด์„œ ํ•ด๋‹น url ์— GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•จ -> ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋Š” response ์— ๋‹ด๊น€

2 block : ๋ฐ›์•„์˜จ response ๋Š” dictionary ํ˜•ํƒœ์˜€๊ธฐ์— key ๋ฅผ ์จ์„œ value ๋ฅผ ์ทจํ•จ.

์ด๋•Œ, ==๋ฐ›์•„์˜จ response ๊ฐ€ ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ์— ๋‹ด๊ฒจ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, console ๋กœ ํ™•์ธ== ํ•ด๊ฐ€๋ฉด์„œ, ํ•˜๋‚˜์”ฉ ๋“ค์–ด๊ฐˆ ํ•„์š”๊ฐ€ ์žˆ์Œ.

๋งŒ์•ฝ, ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ console ๋กœ ํ™•์ธํ–ˆ๋‹ค๋ฉด -> ==๊ทธ๊ฑธ let rows ๊ฐ™์€ ๋ณ€์ˆ˜์—== ๋‹ด๋Š”๋‹ค.

3 block

(let i = 0; i < rows.length; i++ ์ด ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•˜๋Š”๊ฑด์ง€ ์•„๋Š” ๊ฑด ์ค‘์š”. โญโญโญ

๋ฐ˜๋ณต๋ฌธ์—์„œ i ๋Š” '๋ฐฐ์—ด ์•ˆ์— ๋ฐฐ์น˜๋œ ์ˆœ์„œ' ๋ฅผ ์˜๋ฏธํ•  ๊ฒƒ.

๋ณด๋Š” ์ˆœ์„œ๊ฐ€ 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ํ•ด๋‹น ์—ด์˜ ๋ ๊นŒ์ง€ ๊ฐ€๊ณ , ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋Š” ํ•˜๋‚˜์”ฉ.

๋‚˜๋จธ์ง€ : ๋ฆฌ์ŠคํŠธ์˜ ๋ช‡ ๋ฒˆ์งธ ๋กœ ๊ฐ€์„œ -> ํ•ด๋‹น key ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” value ๋ฅผ ๊ฐ€์ ธ์™€๋ผ -> ๊ทธ๊ฑธ console ๋กœ ๋ณด์—ฌ์ค˜๋ผ ( = f12 ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด๋‹ฌ๋ผ. )



๊ฒฐ๊ณผ๋ฌผ
  • ๊ทธ๋Ÿฌ๋ฉด, '๊ตฌ ์ด๋ฆ„', '๊ตฌ ๋ฏธ์„ธ๋จผ์ง€' ๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์„œ ๋‚˜์˜ด


๐Ÿ’ญ What i learned


  • โญโญโญ console ๋กœ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์ฐํžˆ๋Š” ๊ฑธ ํ™•์ธํ•˜๊ณ  -> ๊ทธ๊ฑธ ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค! ์ด๊ฑด ์Šต๊ด€์ธ๊ฑฐ ๊ฐ™๋‹ค โญโญโญโญโญโญโญ

  • ๋˜ํ•œ ๋ฝ‘์•„๋‚ธ ๊ฐ’์ด ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ฒจ์žˆ๋‹ค๋Š” ๊ฒƒ ๋˜ํ•œ ์•Œ๊ณ  ์žˆ์Œ!

728x90
๋ฐ˜์‘ํ˜•
Comments