JOYLOG

์˜ํ™”์ง„ํฅ์œ„์›ํšŒ API ์‚ฌ์šฉํ•˜๊ธฐ : Fetch API ๋ณธ๋ฌธ

D E V E L O P E R ๐Ÿ’ป/Back-end

์˜ํ™”์ง„ํฅ์œ„์›ํšŒ API ์‚ฌ์šฉํ•˜๊ธฐ : Fetch API

์ฃผํž 2020. 9. 11. 18:12

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š”

Json์œผ๋กœ ์˜ํ™”์ง„ํฅ์œ„์›ํšŒ์˜ ์˜คํ”ˆ API๋ฅผ ํ™œ์šฉํ•  ๊ฒƒ์ด๋‹ค.

โ€‹

htmlํŒŒ์ผ์— static์„ ์—ฐ๊ฒฐํ•˜๊ณ  json ํŒŒ์ผ์„ ์—ฐ๊ฒฐํ•ด๋‘๋ฉด ์ค€๋น„์™„๋ฃŒ!

โ€‹

 

index.html

index.json

 

 

๋‘ ๊ฐœ์˜ ํŒŒ์ผ๊ณผ ์›น๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์ค€๋น„ํ•˜์˜€๊ณ , ์ด๋ฒˆ ํ•™์Šต์—์„œ๋Š” json ํŒŒ์ผ ์ˆ˜์ •๋งŒ ์žˆ์„ ์˜ˆ์ •์ด๋‹ค


 

"์˜ํ™”์ง„ํฅ์œ„์›ํšŒ ์˜คํ”ˆAPI"

โ€‹

http://www.kobis.or.kr/kobisopenapi/homepg/apiservice/searchServiceInfo.do 

 

์˜ํ™”์ง„ํฅ์œ„์›ํšŒ ์˜คํ”ˆAPI

 

www.kobis.or.kr

 

์œ„ ์ฃผ์†Œ, ์˜ํ™”์ง„ํฅ์œ„์›ํšŒ ์˜คํ”ˆAPI์—์„œ ์˜ํ™”๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ด๊ณ ,

ํŽ˜์ด์ง€๋ฅผ ๋ณด๋ฉด ์š”์ฒญ ์ธํ„ฐํŽ˜์ด์Šค, ์‘๋‹ต ๊ตฌ์กฐ ๋“ฑ์ด ์–ด๋–ป๊ฒŒ ์ง€์ •๋˜์–ด ์žˆ๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

์šฐ๋ฆฌ๋Š” ์ด๋ฒˆ ์‹œ๊ฐ„ ์œ„๋ฅผ ํ™œ์šฉํ•ด ์›ํ•˜๋Š” API๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

โ€‹

ํšŒ์›๊ฐ€์ž…์„ ํ•ด์„œ ๋ณธ์ธ์˜ ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•„์•ผ API๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. โญ

key๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์•„์•ผํ•˜๋Š” ์ด์œ ๋Š” ๋‚ด๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ์‹ ๋ถ„์„ ํ™•์ธ๋ฐ›๊ณ  API๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ!

์ผ์ข…์˜ ์‹ ๋ถ„ ํ™•์ธ ํ”„๋กœ์„ธ์Šค๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

 


 

"์ฝ”๋“œ"

โ€‹

 

index.html

<!-- index.html -->
<!-- static ํŒŒ์ผ๋“ค๊ณผ ์—ฐ๊ฒฐํ•˜์˜€๊ณ , API๋กœ ์–ป์€ ์ •๋ณด๋“ค์„ html๋กœ ๋ณด๋‚ด๊ธฐ์œ„ํ•ด id๊ฐ€ a์ธ div๋ฅผ ๋งŒ๋“ค์–ด ๋†“์Œ -->
<!-- script์— json ์—ฐ๊ฒฐ -->


<!DOCTYPE html>
{% load static %}
<!-- static ํด๋”์™€ ์—ฐ๊ฒฐ -->
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="a">
    </div>
    <script src = "/static/js/index.js"> //javaScirpt
    </script>
    
</body>
</html>

 

index.json

>> ์˜ํ™”์ด๋ฆ„, ๊ตญ๊ฐ€, ์žฅ๋ฅด, ์ œ์ž‘์—ฐ๋„๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

์ด ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„ 

JavaScript์—์„œ DOM์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค

 

์•„๋ž˜ ์ฝ”๋“œ์—์„œ๋Š”

document.createElement('p')๋ฅผ ์ด์šฉํ•˜์—ฌ

๊ฐ€์ ธ์˜จ ์ •๋ณด๋“ค์„ ์ƒˆ๋กœ์ƒ๊ธด pํƒœ๊ทธ์— ๋„ฃ์–ด์ฃผ์–ด

html์— ๋ณด๋‚ด๋Š” ์ž‘์—…์„ ํ•˜๊ณ  ์žˆ๋‹ค

 

//index.json

var key = "?key=๊ฐœ์ธ์˜ํ‚ค" // key ์•ž์— <?key=>๋ฅผ ๋ถ™์—ฌ์•ผํ•จ : prameter
var itemPerPage = "&itemPerPage=20" //20๊ฐœ๋ฅผ ๊ฐ€์ ธ์˜ฌ๊ฒƒ์ด๋ผ์„œ
const url = "http://www.kobis.or.kr/kobisopenapi/webservice/rest/movie/searchMovieList.json"
            + key
            + itemPerPage 
var item_int = 20

var title = document.getElementById('a') //html์—์„œ id a์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ
fetch(url)
            .then(response=>response.json())
            .then(function(msg){ //์•„๋ž˜๋ถ€ํ„ฐ๋Š” html๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ
                for(var i =0; i<item_int; i++){
                    var div = document.createElement('div');
                    var low_div = document.createElement('div')
                    var title_div = document.createElement('div')
                    var br = document.createElement('br');

                    var mp = document.createElement('p');
                    mp.id = 'movieNm'
                    var np = document.createElement('p');
                    np.id = 'nationAlt'
                    var gp = document.createElement('p');
                    gp.id = 'genreAlt'
                    var pp = document.createElement('p');
                    pp.id = 'prdtYear'
                    title_div.id = 'title_div'
                    low_div.id = 'low_div'
                    div.id = 'movies'
                    short_url = msg.movieListResult.movieList[i]
                    console.log(short_url);
                    div.appendChild(title_div)
                    div.appendChild(low_div)
                    title.appendChild(div);
                    var movieNm = document.createTextNode(short_url.movieNm);
                    var nationAlt = document.createTextNode(short_url.nationAlt);
                    var genreAlt = document.createTextNode(short_url.genreAlt);
                    var prdtYear = document.createTextNode(short_url.prdtYear);
                    
                    mp.appendChild(movieNm);
                    np.appendChild(nationAlt);
                    gp.appendChild(genreAlt);
                    pp.appendChild(prdtYear);

                    title_div.appendChild(mp);
                    low_div.appendChild(br);
                    low_div.appendChild(np);
                    low_div.appendChild(gp);
                    low_div.appendChild(pp);




                }
                        
            });

 

++ ์ถ”๊ฐ€๋กœ ์ดํ•ด๋ฅผ ๋•๊ธฐ์œ„ํ•ด ๋‹ค๋ฅธ ๋ถ„๊ป˜์„œ

์˜ํ™”์ด๋ฆ„๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด๋‚ด์ฃผ์…จ๋‹ค

 

        fetch(url)
            .then(response=>response.json())
            .then(function(msg){
                // msg๊ฐ€ ์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ๋จน์—ˆ๋‚˜?
                console.log(msg);

                // i<10 ๋‚œ 10๊ฐœ๋งŒ ๋ณ€์ˆ˜๋กœ ๋ถ€์—ฌํ•ด๋„ ๊ดœ์ฐฎ์•„์š” 10๋Œ€์‹ 
                for(let i = 0; i<10; i++){
                    // console์— ์ฐ์€ msg ์†์œผ๋กœ ๋“ค์–ด๊ฐ€๋ณด๋ฉด movieListResult -> MovieList ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”
                    // ํ•ด๋‹น ๋‚ด์šฉ์„ short_url์— ๋‹ด์•„์š” for๋ฌธ์ด๋‹ˆ๊น ๋ณธ์ธ์ด ์›ํ•˜๋Š” ํšŸ์ˆ˜๋งŒํผ
                    short_url = msg.movieListResult.movieList[i];
                    
                    //short_url์ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฒจ๋จน์—ˆ๋‚˜?
                    console.log(short_url);
                    
                    // ์˜ํ™”์ œ๋ชฉ์„ id = "a" divํƒœ๊ทธ์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ short_url ์ค‘์—์„œ movieNm์„ movieNm์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด์•˜์–ด์š”
                    let movieNm = document.createTextNode(short_url.movieNm);
                    title.appendChild(movieNm);

                    // ์ค„๋ฐ”๊ฟˆ์„ ์œ„ํ•œ brํƒœ๊ทธ _ ์—†์–ด๋„๋จ
                    let br = document.createElement('br');
                    title.appendChild(br);

                }
            });

 


"๊ฒฐ๊ณผ"

โ€‹

html์— ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค!

'D E V E L O P E R ๐Ÿ’ป > Back-end' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Mongo DB] 'MongooseServerSelectionError' ํ•ด๊ฒฐ  (2) 2021.07.07
Comments