๐ฅ Ajax๋?
- Asynchronous Javascript And Xml(Json)
- ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฒ์๊ฒ ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๊ณ , ์๋ฒ๊ฐ ์๋ตํ ๋ฐ์ดํฐ๋ฅผ ์์ ํ์ฌ ์นํ์ด์ง๋ฅผ ๋์ ์ผ๋ก ๊ฐฑ์ ํ๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์
- ํ์ํ ๋ถ๋ถ๋ง ๋น๋๊ธฐ ๋ฐฉ์์ผ๋ก ์ ์ก๋ฐ์ ๋ณ๊ฒฝํ ํ์๊ฐ ์๋ ๋ถ๋ถ๋ง ํ์ ์ ์ผ๋ก ๋ ๋๋ง
๐ฅ jQuery ์ด์ฉ
๐งพ JSON
<p id="demo"></p>
<button type="button">button</button>
$("button").click(function() {
$.ajax({
url:"data.json",
type:"get",
datatype:"json",
success:function(data){
for(let i = 0; i < data.length; i++) {
$("#demo).append(data[i].name + " ");
$("#demo).append(data[i].age + " ");
$("#demo).append(data[i].address + " ");
$("#demo).append(data[i].phone + " ");
},
error:function() {
alert("error");
}
}
});
});
[
{
"name":"ํ๊ธธ๋",
"age":24,
"address":"์์ธ์",
"phone":"123"
},
{
"name":"์ฑ์ถํฅ",
"age":16,
"address":"๋จ์์",
"phone":"234"
},
{
"name":"ํ๋๊ป",
"age":22,
"address":"๊ฐ๋ฆ์",
"phone":"345"
}
]
๐งพ JSP
<p id="name"></p>
<p id="age"></p>
<p id="birth"></p>
$(function() {
$("button").click(function() {
$.ajax({
url:"data.jsp",
type:"get",
datatype:"json",
success:function(data) {
let json = JSON.parse(data);
$("#name").text(json.name);
$("#age").text(json.age);
$("#birth").text(json.birth);
},
error:function() {
alert("error");
}
});
});
});
<%
String name = "ํ๊ดํ"
int age = 34;
String birth = "1990/10/31";
String str = "{\"name\":\"" + name + "\", \"age\":\"" + age + "\", \"birth\":\"" + birth + "\"}"
out.println(str);
%>