๋ฐ์ํ
ajax
Asynchronous Javascript And Xml
- ์ฐ๋ฆฌ๊ฐ..๊ตฌ๊ธ์ด๋ ๋ค์ด๋ฒ์์ ๋ญ๊ฐ๋ฅผ ๊ฒ์ํ๋ฉด ๊ฒ์๊ฒฐ๊ณผ์ฐฝ์ด ๋์ค์์์.
- ์์ด์ ์ค๋ ์ฐฝ์ด ๋ฐ๋์ง ์๊ณ ์์ฒ๋ผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
- ์ด๋ฅผ ๋น๋๊ธฐ ํต์ ๊ธฐ๋ฒ์ด๋ผ๊ณ ํฉ๋๋ค.
API Docs
https://api.jquery.com/jQuery.ajax/
json ํํ ๊ธฐ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
- url : ํต์ ํ ์๋ฒ๋ ํ์ผ ์ฃผ์.
- method : get / post ์ค ์ ํ.
- async : default: true (false๋ ๋๊ธฐ์).
- dataType : ํต์ ์ด ์ฑ๊ณตํ๋ฉด ๋ฆฌํด๋ฐ์ data์ type (text, xml, json, ...)
- success : (์ ๋ค๊ฐ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ด) ํต์ ์ฑ๊ณตํ์ ๋ ์คํํ ์ฝ๋๋ฅผ ์ ์ด์ค๋๋ค.
- ์ฌ๊ธฐ์ function(data) : data๋ ์๋ฒ์์ ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ์ ๋๋ค.
- error : ์๋ฌ ์ ์คํ.
๐ ajax๋ ํต์ ์คํจ๋ ์๋ฌ๊ฐ ์๋๋ผ, ์คํจ ์ด์ ๋ฅผ ์ ์ ์์ต๋๋ค.
๋ง์ฝ ํต์ ์คํจ ์ url, method, async, dataType ์ด ๋ค๊ฐ์ง๋ง ๋ค์ ํ์ธํ๋ฉด ๋ฉ๋๋ค.
์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ
์ ์ฒด์ฝ๋
๋๋ณด๊ธฐ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>์์ด์ ์ค</title>
<script type="text/javascript" src="/Java_Study_001/src/main/webapp/common/js/lib/jquery-3.7.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#emp_search").click(function(){
let empid = $("input[name=empid]").val();
if (!isNaN(empid) && empid.length > 2){
$.ajax({
url: "./emplist.xml", // ํต์ ํ ์๋ฒ or ํ์ด์ง
method: "get",
async: true, // ๋น๋๊ธฐ ์ฌ๋ถ (false๋ ๋๊ธฐ์)
dataType: "xml", // ๋ฆฌํด๋ฐ์ ๋ฐ์ดํฐ ํ์
success: function(data){
let empInfo = $(data).find("EMPLOYEE_ID:Contains("+empid+")").parent();
if (empInfo.is("ROW")){
$("table input").each(function(i){
$(this).val($(empInfo).children().eq(i).text());
});
}
},
error: function(request, error){
alert("code: " + request.status+"\n" +
"message: " + request.responseText + "\n" +
"error: " + error);
}
});
}
// if๋ฌธ ์
๋ ฅ๊ฐ ๋ฏธ์ถฉ์กฑ์ด ๋ฐ ๊ฒฝ๊ณ ์ฐฝ
else {
alert("์ฌ์๋ฒํธ๋ฅผ ๋ค์ํ๋ฒ ํ์ธํด ์ฃผ์ธ์!");
}
});
});
</script>
</head>
<body>
<h1>๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ(๋น๋๊ธฐ)</h1>
<fieldset>
<legend>์ฌ์์ ๋ณด ์กฐํ</legend>
<input type="text" name="empid" />
<input type="button" id="emp_search" value="์กฐํ" />
</fieldset>
<table>
<tr>
<th>์ฌ์๋ฒํธ</th>
<td><input type="text" name="" /></td>
</tr>
<tr>
<th>์ด๋ฆ</th>
<td><input type="text" name="" /></td>
</tr>
<tr>
<th>์ด๋ฉ์ผ</th>
<td><input type="text" name="" /></td>
</tr>
<tr>
<th>์ ํ๋ฒํธ</th>
<td><input type="text" name="" /></td>
</tr>
<tr>
<th>์
์ฌ์ผ</th>
<td><input type="text" name="" /></td>
</tr>
</table>
</body>
</html>
๋ฐ์ํ
'โจ UI > ๐ JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS][jQuery] $ ์ ์ด์ฟผ๋ฆฌ๋? ์ ์ฉ/์ฌ์ฉ๋ฒ (0) | 2024.12.28 |
---|---|
[JS]JSON (0) | 2024.12.28 |
[JS]Window ๋ํํ ํจ์ (0) | 2024.12.27 |
[JS]html ์ถ๊ฐ/์์ ํ๊ธฐ(+๋ฒํผ ๋นํ์ฑํ, ์์ฑ๋ณ๊ฒฝ) (1) | 2024.12.27 |
[JS]ํจ์์ ์ข ๋ฅ(์ต๋ช /ํ์ดํ/ํด๋ก์ ) (0) | 2024.12.26 |