Frontend/jQuery & JSTL

[JavaScript] Ajax 사용법

Mr.6_냥아치 2024. 2. 20. 10:42
반응형

Ajax에서 데이터 처리

<!-- JSON파일로 응답처리 -->
$.ajax({
	method: "GET", <!-- 메서드 방식 -->
	contentType: "application/json",
	dataType: "json",
	url: "/test/sample_path/sample_sample.json", <!-- json파일로 데이터 읽어오기 -->
}).done(function (sampleValue) {
})


<!-- JSON Object 값으로 응답처리 -->
$.ajax({
	method: "GET", <!-- 메서드 방식 -->
	url: "/test/sample_url/sample", <!-- json respons로 데이터 읽어오기 -->
}).done(function (sampleValue) {
})

<!-- JSON Object 응답이 아닌경우 Json으로 파싱하여 응답처리 -->
$.ajax({
	method: "GET", <!-- 메서드 방식 -->
	url: "/test/sample_url/sample", <!-- 데이터 읽어오기 -->
}).done(function (obj){
	var sigungu2 = JSON.parse(obj); <!-- 데이터 파싱 -->
})

<!-- JSON Object 응답이 아닌경우 Json으로 Content타입 변경하여 응답처리 -->
$.ajax({
	method: "GET", <!-- 메서드 방식 -->
	contentType: "application/json",
	dataType: "json",
	url: "/test/sample_url/sample", <!-- 데이터 읽어오기 -->
}).done(function (sampleValue) {
})

 


 

Ajax 축약법

<!-- $.get : get요청 -->
$.get("example.php", function(data) {
    alert("Data: " + data);
});

<!--  $.post : post요청  -->
$.post("example.php", { name: "John", time: "2pm" }, function(data) {
    alert("Data: " + data);
});

 

반응형