단순히 Web 화면에서 무언가를 부르거나 데이터를 조회하고 싶은 경우, 페이지 전체를 새로고침하지 않기 위해서 사용한다고 볼 수 있습니다. JSON 이나 XML 형태로 필요한 데이터만 받아 갱신하기 때문에 그만큼의 자원과 시간을 아낄 수 있습니다.
request를 보낼 준비를 브라우저에게 시키는 과정
이것을 위해서 필요한 method를 갖춘 object가 필요합니다.
서버에서 response가 왔을 때 실행시키는 함수
HTML 페이지를 업데이트 합니다.
A callback function is a function which is:
- passed as an argument to another function, and,
- is invoked after some kind of event.
// The callback method
function meaningOfLife() {
log("The meaning of life is: 42");
}
// A method which accepts a callback method as an argument
// takes a function reference to be executed when printANumber completes
function printANumber(int number, function callbackFunction) {
print("The number you provided is: " + number);
}
// Driver method
function event() {
printANumber(6, meaningOfLife);
}
var serverAddress = 'https://hacker-news.firebaseio.com/v0/topstories.json';
//jQuery의 .get 메서드 사용
$.ajax({
url: ,
type: 'GET',
success: function onData(data){
console.log(data);
},
error: function onError(error){
console.error(error);
}
});
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:replace="~{/layout/basic :: setContent(~{this::content} )}">
<th:block th:fragment="content">
<nav class="navbar navbar-expand-lg navbar-light " style="background-color: #F2D7D5;">
<div class="container-fluid">
<a class="navbar-brand" href="#">🌺</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link" aria-current="page" href="#">Home</a>
<a class="nav-link" href="#">Features</a>
</div>
</div>
</div>
</nav>
<div class="container">
<div class="row mt-3">
<div class="card text-center">
<h5 class="card-header">Data transmission using ajax</h5>
<div class="card-body">
<h5 class="card-title">hello! 😀 </h5>
<p class="card-text">
Enter the text you want to send and click the button.</p>
<form class="card-text">
<div class="col-12">
<div class="input-group mb-3">
<input id="input" placeholder="Please enter text" type="text" class="form-control">
<button class="btn btn-outline-secondary" type="button" onclick="dataSend()">submit</button>
</div>
</div>
<div id="resultDiv">
<p th:if="${msg}!=null" th:text="${msg}"></p>
</div>
</form>
<a href="#" class="btn" style="background-color: #EBDEF0">Go github</a>
</div>
</div>
</div>
</div>
</th:block>
</th:block>
</html>

<script th:inline="javascript">
function dataSend(){
const data =$("#input").val();
const sendDTO = {
result:data
};
$.ajax({
url: "/send",
data: sendDTO,
type: 'POST',
}).done(function (data){
$("#resultDiv").replaceWith(data);
});
}
</script>
package com.example.demo.controller;
import com.example.demo.dto.SendDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class AjaxController {
@GetMapping("/")
public String index(){
return "/ajax";
}
@PostMapping("/send")
public String ajaxHome(Model model, SendDto dto){
model.addAttribute("msg",dto.getResult());
return "ajax :: #resultDiv";
}
}
@Data
public class sendDto {
private String msg;
private String Result;
}
