JSON.stringify() 어떤 의미?

devdo·2021년 8월 3일
1

프론트 스터디

목록 보기
5/8

JSON.stringify(객체) : 객체를 String 문자열로 변환해주는 역할을 한다.

그런데 이 문자열은 JSON으로 바로 무방하다. 왜냐하면 이렇게 변경된 문자열은 JSON으로 인코딩된(JSON-encoded)객체이기 때문이다.

예시

function chkDuplication(){
     
var sendObject = {                
    name : $("#name").val(),
    age : $("#age").val()
}

$.ajax({
        url: "${contextPath}/ajax/test",
        type: "post",
        data: JSON.stringify(sendObject),    
        dataType: "json",
        contentType: "application/json; charset=utf-8",
                
        success: function(data) {
              //alert("성공");
          $('.ajaxinfo1').append(data.name);
          $('.ajaxinfo2').append(data.age);
          $('.ajaxinfo3').append(data.phone);
        },
                
         error: function(errorThrown) {
           alert(errorThrown.statusText);
        }
   });

참고사이트 : https://ko.javascript.info/json

profile
배운 것을 기록합니다.

0개의 댓글