๐ป
JavaScript ์ ์ดํด - [jQuery] jQuery.ajax() ๋ณธ๋ฌธ
01_menuAjax.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$('#menu1').click(function(){
$.ajax({
url:"01_menu.html",
type:"GET",
dataType:"html",
success:function(data){
$('#message1').html(data);
}
});
});
$('#menu2').click(function(){
$.ajax({
url:"01_menu.html",
type:"GET",
dataType:"html",
success:function(data){
$('#message2').html($(data).children("ul > li"));
}
})
});
});
</script>
</head>
<body>
<div>
<a href="#" id="menu1">๋ฉ๋ด๋ณด๊ธฐ 1</a>
<span id="message1"></span>
</div>
<br/><br/>
<div>
<a href="#" id="menu2">๋ฉ๋ด๋ณด๊ธฐ 2</a>
<span id="message2" style="color:red"></span>
</div>
</body>
</html>
01_menu.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery Ajax</title>
</head>
<body>
<p>์ค์</p>
<ul>
<li>์ง์ฅ๋ฉด</li>
<li>์งฌ๋ฝ</li>
<li>๊ธฐ์ค๋ฉด</li>
<li>ํ์์ก</li>
</ul>
<p>๋ฉ๋ด๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์. </p>
</body>
</html>
02_strAjax.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url:"02_str.txt",
type:"GET",
dataType:"text",
success: function(data, textStatus, jqXHR){
//alert(textStatus + " " + jqXHR); //success [object Object]
$('#resultDisplay').text(data);
},
error: function(jqXHR, textStatus, errorThrown){
var arr = new Array();
arr.push(jqXHR);
arr.push(textStatus);
arr.push(errorThrown);
//alert(arr.join("\n")); //[object Object] error
}
});
});
</script>
</head>
<body>
<div id="resultDisplay"></div>
</body>
</html>
02_str.txt
์๋
ํ์ธ์. AJAX 1234
03_idCheck.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
${param.id}
${param.password}
03_idCheckGet.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
var id = $('#id').val();
var password = $('#password').val();
var sendData = "id=" + id + "&password=" + password;
//alert(sendData);
$.ajax({
url : "03_idCheck.jsp?" + sendData,
type : "GET",
dataType: "text",
success : function(data){
$("#resultDisplay").text(data);
}
});
});
});
</script>
</head>
<body>
<form id="createForm">
<label>์์ด๋</label>
<input type="text" name="id" id="id"/><br/>
<label>๋น๋ฐ๋ฒํธ</label>
<input type="password" name="password" id="password"/><br/>
<input type="button" id="btn" value="์์ฒญ"/>
</form>
<div id="resultDisplay"></div>
</body>
</html>
03_idCheckPost.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$('#btn').click(function(){
var id = $('#id').val();
var password = $('#password').val();
var sendData = "id=" + id + "&password=" + password;
//alert(id + password+ sendData);
$.ajax({
url : "03_idCheck.jsp",
type : "POST",
//data : sendData,
data : {id:id, password:password},
//data : {"id":id , "password":password},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "text",
success : function(data){
$('#resultDisplay').text(data);
}
});
});
});
</script>
</head>
<body>
<form id="createForm">
<label>์์ด๋</label>
<input type="text" name="id" id="id"/><br/>
<label>๋น๋ฐ๋ฒํธ</label>
<input type="password" name="password" id="password"/><br/>
<input type="button" id="btn" value="์์ฒญ"/>
</form>
<div id="resultDisplay"></div>
</body>
</html>
04_JSON.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: "04_JSON.txt",
type: "GET",
dataType: "json",
success : function(data){
var memberList = data.member;
//alert(memberList.length);
$(memberList).each(function (i){
var member = memberList[i];
var html = "<span>" + member.stdNumber + "</span> ";
html += "<span>" + member.stdName +"</span> ";
html += "<span>" + member.score +"</span><br/>";
$('#resultDisp').append(html);
});
}
});
});
</script>
</head>
<body>
<div id="resultDisp"></div>
</body>
</html>
04_JSON.txt
{"member" : [{"stdNumber":1, "stdName":"ํ๊ธธ๋", "score":95},
{"stdNumber":2, "stdName":"๊น๊ธธ๋", "score":90},
{"stdNumber":3, "stdName":"๋ฐ๊ธธ๋", "score":85}]
}
05_XML.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JQuery AJAX</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: "05_XML.xml",
type: "GET",
dataType: "xml",
success : function(data){
var studentList = $(data).find('student');
alert(studentList.length);
$(studentList).each(function (i){
var student = studentList.eq(i);
var html = "<span>" + student.children("stdNumber").text() + "</span> ";
html += "<span>" + student.children("stdName").text() + "</span> ";
html += "<span>" + student.children("score").text() + "</span><br/>";
$('#resultDisp').append(html);
});
}
});
});
</script>
</head>
<body>
<div id="resultDisp"></div>
</body>
</body>
</html>
05_XML.xml
<?xml version="1.0" encoding="UTF-8"?>
<member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/05_XML 05_XML.xsd"
xmlns="http://www.example.org/05_XML">
<student>
<stdNumber>1001</stdNumber>
<stdName>ํ๊ธธ๋</stdName>
<score>95</score>
</student>
<student>
<stdNumber>1002</stdNumber>
<stdName>๊น๊ธธ๋</stdName>
<score>90</score>
</student>
<student>
<stdNumber>1003</stdNumber>
<stdName>๋ฐ๊ธธ๋</stdName>
<score>80</score>
</student>
</member>
๋ฐ์ํ
'KITRI > JAVASCRIPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์ ์ดํด - [jQuery] JSON ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค์ด/ ํ์ค ๋๊ธ ๋ฌ๊ธฐ ์์ (ajax๋ก ๋ฐ๊พธ๊ธฐ) (0) | 2020.07.23 |
---|---|
JavaScript ์ ์ดํด - [jQuery] Animate (0) | 2020.07.23 |
JavaScript ์ ์ดํด - [jQuery] Effects : Hide, Toggle, Slide, Fade (0) | 2020.07.22 |
JavaScript ์ ์ดํด - [jQuery] Theme (0) | 2020.07.22 |
JavaScript ์ ์ดํด - [jQuery] DOM ํจ์(2) (0) | 2020.07.22 |
Comments