Ajax2

js015.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์ž.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-4">
                <img src="https://via.placeholder.com/600" class="w-100">
                <h5>Card title</h5>
                <p>๊ฐ€๊ฒฉ : 70000</p>
            </div>
        </div>
    </div>

    <script>
        var products = [
            { id : 0, price : 70000, title : 'Blossom Dress' },
            { id : 1, price : 50000, title : 'Springfield Shirt' },
            { id : 2, price : 60000, title : 'Black Monastery' }
        ];
    </script>

</body>
</html>

โœจ ์‹ค์Šต. <div class="row"> ์•„๋ž˜์˜ div ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  script๋กœ products์˜ ๊ฐฏ์ˆ˜๋งŒํผ card ์ƒ์„ฑ

์ œ๋ฐœ ์ง์ ‘ ์งœ๋ณด๊ธฐ ๐Ÿคฃ

 products.forEach(function (item,index){
      $('.row').append(`<div class="col-sm-4">
           <img src="https://via.placeholder.com/600" class="w-100">
           <h5>${item.title}</h5>
           <p>๊ฐ€๊ฒฉ : ${item.price}</p>
       </div>`)
   });

โœจ ์‹ค์Šต. ์ƒํ’ˆ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด Ajax ๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์™€์„œ ์ฝ˜์†”์ฐฝ์— ์ถœ๋ ฅ

๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ฌ ์„œ๋ฒ„๋Š” ์•„๋ž˜ URL ์‚ฌ์šฉ

https://gist.githubusercontent.com/leeanJP/3ee931feb393f95895bd652344d1bf13/raw/d255a58907156433ef61de8401bf16564df4407f/more1.json

๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด json ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ string์œผ๋กœ ๋ฐ›์•„์˜จ๋‹ค.

์ด๋Ÿด๋•Œ๋Š” JSON.parse(string) ํ•จ์ˆ˜๋กœ string > json ํ˜•๋ณ€ํ™˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

(์ด์ „์— parseInt parseFloat ํ•จ์ˆ˜ ์ฒ˜๋Ÿผ ํ˜•๋ณ€ํ™˜์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ)

$('#more').on('click',function (){
    $.get('https://gist.githubusercontent.com/leeanJP/3ee931feb393f95895bd652344d1bf13/raw/b65cef1db551c3815352418ca885fb0ff38684d5/more1.json')
        .done(function (result) {
            console.log(typeof result);
            console.log(result);
            var jsonData = JSON.parse(result)
            console.log(typeof jsonData);
            console.log(jsonData);

        }).fail(function (){
        console.log("์—๋Ÿฌ ๋ฐœ์ƒ");
    })
})

โœจ ์‹ค์Šต. ์œ„์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋กœ ์นด๋“œ 3๊ฐœ ๋งŒ๋“ค๊ธฐ

jsonData.forEach(function (item){
    $('.row').append(`<div class="col-sm-4">
        <img src="https://via.placeholder.com/600" class="w-100">
        <h5>${item.title}</h5>
        <p>๊ฐ€๊ฒฉ : ${item.price}</p>
        </div>`)
})

์ง€๊ธˆ์€ ์„œ๋ฒ„์™€ DB๊ฐ€ ๋”ฐ๋กœ ์—†์–ด์„œ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ ํ–ˆ์ง€๋งŒ ๋‚˜์ค‘์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ java Spring

๊นŒ์ง€ ๋ฐฐ์šฐ๊ณ  ๋‚˜๋ฉด ๋‚ด PC์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ์‹์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๊ฒŒ ๋ ๊ฒƒ์ž„.

๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์ด ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ , ์ฒ˜์Œ์—๋Š” 0๋ฒˆ๋ถ€ํ„ฐ 19๋ฒˆ๊นŒ์ง€ 20๊ฐœ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์ƒํ’ˆ๋”๋ณด๊ธฐ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์ƒํ’ˆ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ํšŸ์ˆ˜๋ฅผ ๊ธฐ์–ต ํ–ˆ๋‹ค๊ฐ€ 20๋ฒˆ๋ถ€ํ„ฐ 39๋ฒˆ๊นŒ์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋งˆ์ง€๋ง‰์— ๊ฐ€์„œ๋Š” ์ƒํ’ˆ ๋”๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ์ˆจ๊ธฐ๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๊ฒŒ ์šฐ๋ฆฌ๊ฐ€ ๋ณด๋Š” ์‡ผํ•‘๋ชฐ์˜ 20๊ฐœ์”ฉ ์ƒํ’ˆ๋ณด๋Š” ํŽ˜์ด์ง€์˜ ์ž‘๋™๋ฐฉ์‹

20๊ฐœ์”ฉ ๋ณด๊ธฐ > 50๊ฐœ์”ฉ ๋ณด๊ธฐ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ์ดํ„ฐ ๊ฐฏ์ˆ˜๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ ๊ฐ™๋‹ค.

ํ˜„์—…์—์„œ ์‚ฌ์šฉํ•˜๋Š” Ajax ์ฝ”๋“œ ์˜ˆ์ œ1

$.ajax({
    type : 'post',           // ํƒ€์ž… (get, post, put ๋“ฑ๋“ฑ)
    url : '/test',           // ์š”์ฒญํ•  ์„œ๋ฒ„url
    async : true,            // ๋น„๋™๊ธฐํ™” ์—ฌ๋ถ€ (default : true)
    headers : {              // Http header
      "Content-Type" : "application/json",
      "X-HTTP-Method-Override" : "POST"
    },
    dataType : 'text',       // ๋ฐ์ดํ„ฐ ํƒ€์ž… (html, xml, json, text ๋“ฑ๋“ฑ)
    data : JSON.stringify({  // ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ (Object , String, Array)
      "no" : no,
      "name" : name,
      "nick" : nick
    }),
    success : function(result) { // ๊ฒฐ๊ณผ ์„ฑ๊ณต ์ฝœ๋ฐฑํ•จ์ˆ˜
        console.log(result);
    },
    error : function(request, status, error) { // ๊ฒฐ๊ณผ ์—๋Ÿฌ ์ฝœ๋ฐฑํ•จ์ˆ˜
        console.log(error)
    }
})

ํ˜„์—…์—์„œ ์‚ฌ์šฉํ•˜๋Š” Ajax ์ฝ”๋“œ ์˜ˆ์ œ2

var httpFileUpload = {
    
    getAddr : function(fileElement, callback){
        
        const file = fileElement[0];
        if(!file){
            throw new Error("file Element is required");
        } else {    
            const uploadFile = file.files[0];
            
            if(!uploadFile || !uploadFile.size || !uploadFile.name || uploadFile.size === 0){
                throw new Error("upload file is required");
            } else {
                console.log("file size : " + uploadFile.size+"\nfile name : " + uploadFile.name);
                 
                var params = {
                        filename : uploadFile.name,
                        filesize : uploadFile.size,
                        provider : "TEST"
                    };
                
                $.ajax({
                    type    : "POST",
                    url        : "/sample.do",
                    contentType: "application/json",
                    dataType:"json",
                    data     : JSON.stringify(params)    
                })
                .done(function (data, textStatus, xhr) {
                    console.log(xhr);
                    if(data.result_cd == "1"){
                        alert("success!");
                    } else {
                        alert("์—๋Ÿฌ๋ฐœ์ƒ["+data.result_cd+"]");
                        console.log(data.result_msg);
                        callback(data);
                    }
                })
                .fail(function(data, textStatus, errorThrown){
                    console.log("fail in get addr");
                    callback(data);
                });
            }
        }
    }
}

Last updated