[jQuery] $.getJSON 으로 JSON 출력하기

$.getJSON 사용법

 

index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>

    $(document).ready(function(){

        $.getJSON('/json', function(data){

			//JSON을 순회화여 출력한다
            $.each(data, function(index, obj){
                $('body').append('<h1>' + obj.id + ' : ' + obj.name + '</h1>');
            })

        });
    });

</script>


<body>

</body>
</html>

JSON파일( person.json )

[
    {
      "id": "1",
      "name": "Tiger Nixon"
    },
    {
      "id": "2",
      "name": "joy"
    },
    {
      "id": "3",
      "name": "young"
    }
]

http://localhost:8080/ 결과화면

댓글

Designed by JB FACTORY