javascript asynch using ajax - pai-plznw4me/django-initializer GitHub Wiki
ajax ๋ javascript ์ ํ์ฉํด xml ํ์์ผ๋ก ๋น๋๊ธฐ ํต์ ์ ํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
์๋ ์์๋ฅผ ํตํด ๋ฒํผ์ ๋๋ฆฌ๋ฉด ์๋ก์ด tag ์ ์ฝ์
ํ๋ ์์ ๋ฅผ ๋ณด์ฌ์ค๋ค.
๋น์ฐํ๊ฒ๋ HMTL ๋ถ๋ถ๊ณผ Django ๋ด view ๋ถ๋ถ์ ์์ฑํด์ผ ํ๋ค.
<!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>

html page ์์ url: "{% url 'ajax_app:ajax_test'%}",
์ด ๋ถ๋ถ์ด ์์ฒญ ๋ url ์ด๋ค.
step 1: ์์ฒญ ์ฑ๊ณต
or ์์ฒญ ์คํจ
-> step 2: always
})
.done(function (response) {
alert("์์ฒญ ์ฑ๊ณต");
$("#reply").html(response["hello"]);
})
์์ฒญ ์ฑ๊ณต ์ tag id
๊ฐ reply ์ธ tag ๋ด html tag ์ ์ฝ์
ํ๋ค๊ณ ํ๋ ๊ฒ์ด๋ค.
})
.fail(function () {
alert("์์ฒญ ์คํจ");
})
.always(function () {
alert("์์ฒญ ์๋ฃ");
});
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 ํ๋ค๋ ๊ฒ์ ์์ ์๋ค.
<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>
์ 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 ๋๋ค.
@csrf_exempt
def ajax_form_test(request):
if request.method == 'POST':
data = {"hello": "form data send complete"}
return JsonResponse(data)
- form ๋ฐ์ดํฐ๋ฅผ javascript Array Type ์ผ๋ก ์ถ๊ฐํ๋ค. ๊ทธ ํ ์ถ๊ฐ์ ์ผ๋ก ๋ฃ๊ณ ์ถ์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด Array ๋ก ์ถ๊ฐํ ์ ์๋ค.
$(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("์์ฒญ ์ข
๋ฃ");
});
});
});
request.POST['username']
view ์์๋ ์์ ๊ฐ์ด ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
-
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>