본문 바로가기
목차
자바스크립트

JavaScript - jQuery Ajax (1/2),(2/2)

by 지각생 2022. 1. 31.
728x90

출처:

https://www.youtube.com/watch?v=XbeEc_uRCsY 


jquery ajax 홈페이지 예제

 $.ajax({
      //회원가입 수행 요청
      type: "POST",
      url: "/joinForm",
      data: JSON.stringify(data), //http body 데이터
      contentType: "application/json; charset=utf-8", //body데이터가 어떤 타입인지(MIME)
      dataType: "json", // 요청을 서버로 해서 응답이 왔을 때 기본적으로 모든 것이 문자열(생긴게 json이라면)=> javascript 오브젝트로 변경
    })
      .done(function (resp) {
        alert("회원가입이 완료되었습니다.");
        //console.log(resp);
        location.href = "/";
      })
      .fail(function (error) {
        alert(JSON.stringify(error));
      });

 

 


출처:

https://www.youtube.com/watch?v=jQhzL1nkk2s 

id="execute" 를 클릭하면 발동한다.

let index = {
  init: function () {
    $("#btn-save").on("click", () => {
      //()=>{} this를 바인딩하기 위해서!
      this.save();
    });
    $("#btn-update").on("click", () => {
      //()=>{} this를 바인딩하기 위해서!
      this.update();
    });
    // $("#btn-login").on("click", () => {
    //   //()=>{} this를 바인딩하기 위해서!
    //   this.login();
    // });
  },

내가 사용중인 예제와 비교 중

 

31행 data에서 14행 23행 form값 가져온다.

 

이렇게

 

JSON처리법

 

8행 dataType이 중요하다. json타입이라고 명시.

JSON데이터 출력중이다.

 

내가 사용중인 예제와 조금 다른 형태이다.

728x90

댓글