javascript asynch using ajax - pai-plznw4me/django-initializer GitHub Wiki

Ajax๋กœ ๋น„๋™๊ธฐ ํ†ต์‹  ๋ณด๋‚ด๊ธฐ

ajax ๋ž€ javascript ์„ ํ™œ์šฉํ•ด xml ํ˜•์‹์œผ๋กœ ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.
์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆฌ๋ฉด ์ƒˆ๋กœ์šด tag ์„ ์‚ฝ์ž…ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

๋‹น์—ฐํ•˜๊ฒŒ๋„ HMTL ๋ถ€๋ถ„๊ณผ Django ๋‚ด view ๋ถ€๋ถ„์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

1.์ผ๋ฐ˜์ ์ธ Json ๋ฐ์ดํ„ฐ๋ฅผ Ajax ํ†ต์‹ ์œผ๋กœ ์„œ๋ฒ„์— ๋ณด๋‚ด๊ธฐ ๋ฐ ๋ฐ›๊ธฐ

1.1 HTML Part

<!DOCTYPE html>
<!--ajax_app/templates/simple_page.html-->
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <title>jQuery Ajax Method</title>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script>
      $(function () {
        $("#requestBtn").on("click", function () {
          $.ajax({
            url: "{% url 'ajax_app:ajax_test'%}",
            data: { name: "ํ™๊ธธ๋™" },
            method: "POST",
            dataType: "json",
          })
            .done(function (response) {
              alert("์š”์ฒญ ์„ฑ๊ณต");
              $("#reply").html(response["hello"]);
            })
            .fail(function () {
              alert("์š”์ฒญ ์‹คํŒจ");
            })
            .always(function () {
              alert("์š”์ฒญ ์™„๋ฃŒ");
            });
        });
      });
    </script>
  </head>

  <body>
    <h1>$.ajax() ๋ฉ”์†Œ๋“œ</h1>
    <button id="requestBtn">$.ajax() ๋ฉ”์†Œ๋“œ ์‹คํ–‰</button>
    <div id="reply"></div>
    >
    <p id="text"></p>
  </body>
</html>
ss2

1.1.1

html page ์•ˆ์— url: "{% url 'ajax_app:ajax_test'%}", ์ด ๋ถ€๋ถ„์ด ์š”์ฒญ ๋  url ์ด๋‹ค.

1.1.2 ์š”์ฒญ ํ•œ ํ›„ ํฌ๊ฒŒ 2๊ฐ€์ง€๋กœ ๋‚˜๋‰˜์–ด ์ง„๋‹ค.

step 1: ์š”์ฒญ ์„ฑ๊ณต or ์š”์ฒญ ์‹คํŒจ -> step 2: always

1.1.2.1 ์š”์ฒญ ์„ฑ๊ณต

          })
            .done(function (response) {
              alert("์š”์ฒญ ์„ฑ๊ณต");
              $("#reply").html(response["hello"]);
            })

์š”์ฒญ ์„ฑ๊ณต ์‹œ tag id๊ฐ€ reply ์ธ tag ๋‚ด html tag ์„ ์‚ฝ์ž… ํ•œ๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

1.1.2.2 ์š”์ฒญ ์‹คํŒจ ์‹œ

          })
            .fail(function () {
              alert("์š”์ฒญ ์‹คํŒจ");
            })

1.1.2.3 always

            .always(function () {
              alert("์š”์ฒญ ์™„๋ฃŒ");
            });

1.2 View

django view ๋‚ด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

์ค‘์š”ํ•˜๊ฒŒ ๋ด์•ผ ํ•  ์ ์€ Json์œผ๋กœ return ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

# ajax_app/views.py
@csrf_exempt
def ajax_test(request):
    if request.method == 'POST':
        data = {"hello" : "<a href='#'>'Hello Ajax' from server</a>"}
        return JsonResponse(data)

์—ฌ๊ธฐ์„œ ๋ณด๋ฉด html tag ์„ string ์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค์Œ json ์œผ๋กœ wrapping ํ•ด์„œ return ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์ˆ˜ ์žˆ๋‹ค.

2. Ajax send form data

2.1 HTML

<script src="/static/ajax_app/click_ajax.js"></script>

<!-- ajax form ์ „์†ก test -->
<h1>$.ajax() Form ์ „์†ก ๋ฉ”์†Œ๋“œ</h1>
    <form method="post" id="AjaxFormTest">
    <input type="checkbox" name="ajax" value="radio-button"> ajax form data
</form>
<button id="submit"> ajax form data ์ „์†ก</button>
<div id="reply-form"></div>
<p id="replay-form-render-here"></p>

2.2 Javascript

์œ„ html ์— <script src="/static/ajax_app/click_ajax.js"></script> ์™€ ์—ฐ๊ฒฐ

$(function () {
$("#submit").on("click", function () {
    var form = $('#AjaxFormTest').serialize()
    console.log(form)
  $.ajax({
    url: "/ajax/ajax_form_test/",
    data: form,
    method: "POST",
    dataType: 'json'
  })
    .done(function (response) {
      alert("์š”์ฒญ ์„ฑ๊ณต");
      $("#reply").text(response["hello"]);
    })
    .fail(function () {
      alert("์š”์ฒญ ์‹คํŒจ");
    })
    .always(function () {
      alert("์š”์ฒญ ์™„๋ฃŒ");
    });
  });
});
  • form ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ๋•Œ var form = $('#AjaxFormTest').serialize() ํ•ด๋‹น ํŒŒํŠธ๋ฅผ ๋ณด๋ฉด ์ •๋ณด๋ฅผ serialization ํ•˜๋Š” ๊ฑธ ๋ณผ์ˆ˜ ์žˆ๋Š”๋ฐ
<input type="checkbox" name="ajax" value="radio-button"> ajax form data

์œ„ checkbox html tag ๋ฅผ ์„ ํƒํ•˜๊ฒŒ ๋˜๋ฉด ajax=radio-button ํ˜•ํƒœ๋กœ serialize ๋œ๋‹ค.

2.3 View

@csrf_exempt
def ajax_form_test(request):
    if request.method == 'POST':
        data = {"hello": "form data send complete"}
        return JsonResponse(data)

3. Ajax send form data with Array

  • form ๋ฐ์ดํ„ฐ๋ฅผ javascript Array Type ์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค. ๊ทธ ํ›„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋„ฃ๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด Array ๋กœ ์ถ”๊ฐ€ํ• ์ˆ˜ ์žˆ๋‹ค.

3.1 javascript part

Javascript part

$(function () {
    $("#search_todo_button").on("click", function () {
        /*
        *   Description:
        *   checklist ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š” form ์ •๋ณด๋ฅผ ajax,post ๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
        *   <form id=search_checklist_form> </form> ํ•ด๋‹น ํ…Œ๊ทธ ์•ˆ input tag ์˜ form ์ •๋ณด๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
        *   view function school_management_app:search_checklist ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
        *   return ๊ฐ’์œผ๋กœ ๋ฐ›์€ ์ •๋ณด๋Š” show_checklist.html ๋‚ด tbody ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.
        *   <table><tbody id=checklist_tbody><tbody></table>
        *   TODO: ์ถ”ํ›„์—๋Š” ๊ฐ’๋งŒ ๋ฐ›์•„์„œ javascript ์—์„œ parsing ํ•˜๋„๋ก ์„ค์ •
        * */

        // form ์ •๋ณด๋ฅผ javascript Array ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
        // example) [{name:'name' value: 'song'} ... ]
        let form = $('#search_todo_form').serializeArray() // <---- โ‘  form ์ •๋ณด๋ฅผ Array ๋กœ ๋ณ€ํ™˜ํ•จ

        // top-navi ์— ์žˆ๋Š” ์œ ์ € ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
        let username = $('#right-top-username').text()  

        // ๊ธฐ์กด์˜ form ์ •๋ณด์— username ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
        form.push({name: "username", value: username})  // <---- โ‘ก Array ์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•จ

        $.ajax({
            cache: false,
            url: "http://0.0.0.0:8000/todo/search_todo",
            data: form,
            method: "POST",
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
        }).done(function (response) {
            alert("์š”์ฒญ ์Šน์ธ");
            $("#todo_tbody").html(response["content"]);
        }).fail(function () {
            alert("์š”์ฒญ ์‹คํŒจ");
        }).always(function () {
            alert("์š”์ฒญ ์ข…๋ฃŒ");
        });
    });
});

3.2 view.py

request.POST['username']

view ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.

3.3 Html

  • search_todo_button ์„ ์ค‘์ ์ ์œผ๋กœ ๋ด์•ผ ํ•œ๋‹ค.
<form class="mg-b-20" action="{% url 'todo:search_todo' %}"
      id="search_todo_form" method="post">
    <div class="row gutters-8">
        <div class="col-lg-4 col-12 form-group"> {# ๋‹ด๋‹น์ž ๊ฒ€์ƒ‰ #}
            <input type="text" placeholder="๋‹ด๋‹น์ž" class="form-control" name="assignee">
        </div>
        <div class="col-lg-3 col-12 form-group"> {# ์„ค๋ช… ๊ฒ€์ƒ‰ #}
            <input type="text" placeholder="์„ค๋ช…" class="form-control" name="desc">
        </div>
        <div class="col-lg-3 col-12 form-group"> {# ์‹œ์ž‘ ๋‚ ์งœ ๊ฒ€์ƒ‰ #}
            <input type="text" placeholder="์‹œ์ž‘ ๋‚ ์งœ ex)2019-05-23" class="form-control"
                   name="start_date">
        </div>
        <div class="col-lg-3 col-12 form-group"> {# ์ข…๋ฃŒ ๋‚ ์งœ ๊ฒ€์ƒ‰ #}
            <input type="text" placeholder="์ข…๋ฃŒ ๋‚ ์งœ ex)2019-05-23" class="form-control"
                   name="end_date">
        </div>
        <div class="col-lg-3 col-12 form-group"> {# ์™„๋ฃŒ ์—ฌ๋ถ€ #}
            <input type="text" placeholder="์™„๋ฃŒ์—ฌ๋ถ€ ex) True" class="form-control" name="complete">
        </div>

        <div class="col-lg-2 col-12 form-group">
            <div class="fw-btn-fill btn-gradient-yellow" id="search_todo_button">SEARCH</div>
        </div>
    </div>
</form>
โš ๏ธ **GitHub.com Fallback** โš ๏ธ