๊ธฐ๋ณธ์์ฒญ ์์
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Ajax ๊ณต๋ถ์ค</title>
<!--jquery์ฌ์ฉ์ ์ํ CDN-->
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<h1>GET ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="GetRequest()">GET</button>
<p id="get"></p>
<h1>POST ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="PostRequest()">POST</button>
<p id="post"></p>
<script>
function GetRequest() {
//$.ajax("url") ์ด๊ฒ๋ง ํด๋๋จ
//ajax๋ฉ์๋
$.ajax({
url:"/study/sample.txt",
//๋ณด๋ผ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด jsonํ์์ผ๋ก ์ ๋ฌ
//data:{}
type:"GET",
//๋ฐ์ ๋ฐ์ดํฐ์ ํ์
์ง์
dataType:"text"
})
//์์ฒญ์ ์ฑ๊ณตํ๋ฉด done์ผ๋ก data๊ฐ ์ ๋ฌ๋จ
//status๋ success / fail ์ฌ๋ถ๋ฅผ return
.done(function(data, status){
document.getElementById('get').innerHTML = data;
})
}
function PostRequest(){
$.ajax({
url:"/study/sample.txt",
//data:{}
type:"POST",
dataType:"text"
})
.done(function(data, status){
document.getElementById('post').innerHTML = data;
})
}
</script>
</body>
</html>
data์ ์ก / data์์ฒญ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Ajax ๊ณต๋ถ์ค</title>
<!--jquery์ฌ์ฉ์ ์ํ CDN-->
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<textarea id='text'></textarea>
<h1>GET ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="GetRequest()">GET</button>
<p id="get"></p>
<h1>POST ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="PostRequest()">POST</button>
<p id="post"></p>
<script>
function GetRequest() {
$.ajax({
url:"/study/sample.php",
data: {name:document.getElementById('text').value},
type:"GET"
})
.done(function(data){
document.getElementById('get').innerHTML = data;
})
}
function PostRequest(){
$.ajax({
url:"/study/sample.php",
data: {name:document.getElementById('text').value},
type:"POST"
})
.done(function(data){
document.getElementById('post').innerHTML = data;
})
}
</script>
</body>
</html>
get๊ณผ post๋ฉ์๋๊ฐ ๋ฐ๋ก ์์
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Ajax ๊ณต๋ถ์ค</title>
<!--jquery์ฌ์ฉ์ ์ํ CDN-->
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<h1>GET ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="GetRequest()">GET</button>
<p id="get"></p>
<h1>POST ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="PostRequest()">POST</button>
<p id="post"></p>
<script>
function GetRequest() {
//$.get("url",์ฝ๋ฐฑํจ์(data,status))
$.get("/study/sample.txt",function(data){
document.getElementById('get').innerHTML = data;
})
}
function PostRequest(){
//$.post("url",๋ณด๋ผdata,์ฝ๋ฐฑํจ์(data,status))
//๋ณด๋ผdata๋ ์๋ต๊ฐ๋ฅ ํ๊ฑฐ๊ฐ์๋ฐ ๋ฐ์ดํฐ ์์ผ๋ฉด post์์
$.post("/study/sample.txt",function(data){
document.getElementById('post').innerHTML = data;
})
}
</script>
</body>
</html>
data์ ์ก / data์์ฒญ
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Ajax ๊ณต๋ถ์ค</title>
<!--jquery์ฌ์ฉ์ ์ํ CDN-->
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<textarea id='text'></textarea>
<h1>GET ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="GetRequest()">GET</button>
<p id="get"></p>
<h1>POST ๋ฐฉ์ ์์ฒญ</h1>
<button type="button" onclick="PostRequest()">POST</button>
<p id="post"></p>
<script>
function GetRequest() {
$.get(
"/study/sample.php",
{name:document.getElementById('text').value},
function(data){
document.getElementById('get').innerHTML = data;
})
}
function PostRequest(){
$.post("/study/sample.php",
{name:document.getElementById('text').value},
function(data){
document.getElementById('post').innerHTML = data;
})
}
</script>
</body>
</html>
etc...
.done(function() {
//์์ฒญ ์ฑ๊ณต์ ์คํํ๋ ๋ฉ์๋
})
.fail(function() {
//์์ฒญ ์คํจ์ ์คํํ๋ ๋ฉ์๋
//errorThrown(์ธ๋ฒ์งธ ํ๋ผ๋ฏธํฐ) ์ด์ฉํด ์ค๋ฅ ๋์ถ ๊ฐ๋ฅ
})
.always(function() {
//์ฑ๊ณต๊ณผ์คํจ ์ฌ๋ถ์ ์๊ด์์ด ์คํํ๋ ๋ฉ์๋
});
'๐ Programming > Web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Fetch API๋ก JSON Parsing (0) | 2021.02.16 |
---|---|
[JavaScript] Ajax XMLhttpRequest (0) | 2021.02.07 |
[HTML] ๊ธฐ์ด์ ๋ฆฌ (0) | 2021.01.31 |
[HTML] table & list (0) | 2021.01.31 |