728x90
1. Ajax๋?
Ajax๋ JavaScript์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ค ํ๋์ด๋ฉฐ
Asynchronous Javascript And Xml(๋น๋๊ธฐ์ ์๋ฐ์คํฌ๋ฆฝํธ์ xml)์ ์ฝ์์ด๋ค.
๋ธ๋ผ์ฐ์ ๊ฐ ๊ฐ์ง๊ณ ์๋ XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ์ ์ฒด ํ์ด์ง๋ฅผ ์๋ก ๊ณ ์น์ง ์๊ณ ๋
ํ์ด์ง์ ์ผ๋ถ๋ง์ ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๋ ๊ธฐ๋ฒ ์ด๋ฉฐ
Ajax๋ฅผ ํ๋ง๋๋ก ์ ์ํ์๋ฉด JavaScript๋ฅผ ์ฌ์ฉํ ๋น๋๊ธฐ ํต์ ,
ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ์ XML ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ธฐ์ ์ด๋ผ๊ณ ํ ์ ์๋ค.
Ajax๋ ํ์ฌ ํ๋ฉด์์ ๋ฐ์ดํฐ๋ฅผ ๋์ด์ค๋ ๊ฒ์ด๋ค.
url:"data.jsp",
type:"get", //get/post ->servlet
//data: "t1=XYZ&t2=์๋
ํ์ธ์",
data: {t1:"๊ฐ์์ง", t2:"๊ณ ์์ด"},
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ajax ์์</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<p id = "demo"></p>
<br>
<button>ํด๋ฆญ</button>
<script type="text/javascript">
$(function () {
$("button").click(function () {
//๊ธฐ๋ณธ๋ฌธ๋ฒ
$.ajax({
//๊ฐ๋ ํ์ํ๊ฒ,
//์ด๋ํ ๋send////////////
url:"data.jsp",
type:"get", //get/post ->servlet
//data: "t1=XYZ&t2=์๋
ํ์ธ์",
data: {t1:"๊ฐ์์ง", t2:"๊ณ ์์ด"},
////////////////////////send
///////////////////////๊ฐ๋ค์์๋
//recv
//function ๊ฐ๋ค์์ ๋ฐ์ดํ๋ ํ๋ผ๋ฏธํฐ์ data
success: function(data, status, xhr){
//alert("suc");
//๊ฐ๋ค์์๋ ์ฑ๊ณตํ๋ฉด ๋ฌ๋ค.
//alert(data); //data.jsp ์ ํ์ผ ์ฝ๋๋ฅผ ๋ค ๊ฐ์ ธ์จ๋ค.
$("#demo").html(data);
//alert(status);
//alert(xhr);
alert(xhr.responseText);
},
error:function(/* xhr, status, error */){
alert("error");
},
complete:function(xhr, status){
alert("ํต์ ์ข
๋ฃ");
}
/////////////////////////recv
});
});
});
</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
t1 = <%=request.getParameter("t1") %>
<br>
t2 = <%=request.getParameter("t2") %>
๋๊ธ