1. Ajax

2. Ajax(JQuery 기준)

3. Ajax(최신 버전의 Ajax 방법)

1. Ajax

1-1) Ajax

1-2) Ajax 웹 페이지 동작

Untitled

2. Ajax 문법 (JQuery기준)

2-1) Ajax 옵션(JQuery 기준)

2-2) Ajax 문법(JQuery 기준)

$.ajax({
    type : 'post', // 타입 (get, post, put 등등)
    url : '/test', // 요청할 서버url
    async : true, // 비동기화 여부 (default : true)
    headers : { // Http header
      "Content-Type" : "application/json",
      "X-HTTP-Method-Override" : "POST"
    },
    dataType : 'text', // 데이터 타입 (html, xml, json, text 등등)
    data : JSON.stringify({ // 보낼 데이터 (Object , String, Array)
      "no" : no,
      "name" : name,
      "nick" : nick
    }),
    success : function(result) { // 결과 성공 콜백함수
        console.log(result);
    },
    error : function(request, status, error) { // 결과 에러 콜백함수
        console.log(error)
    }
})

2-3) $.getJson()

$.getJSON( url [, data] [, success] ) {}

// $.getJSON는 아래 ajax함수의 약자 이다.
$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

2-3) $.post()

$.post( url [, data ] [, success ] [, dataType ] ) {}

// $.post는 아래 ajax 함수의 약자이다.
$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

3. Ajax(최신 버전의 Ajax 방법)

$.ajax( "/text",
    {
      method: 'get',
      data : { name: "chan" },
      dataType: 'json'
    }
  )
  .done(function() { // 서버요청이 성공시의 콜백함수
    alert( "success" );
  })
  .fail(function() { // 서버요청이 에러시의 콜백함수
    alert( "error" );
  })
  .always(function() { // 항상 실행 (finally 같은느낌)
    alert( "complete" );
  });