본문 바로가기
목차
Spring/Spring boot

왜 결과값 없는 메소드(insert나 delete등)에 void타입을 쓰지 않는걸까?(공부중..)

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

아직 확실하겐 아니지만

 

ajax done()에서는 어떠한 거라도 응답이 있어야지 인식때문에 int타입 같은걸로 뭐라도 값을 반환해 주는 듯하다. 

 

 

package com.MyBlog.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.MyBlog.Dto.UserDto;
import com.MyBlog.Service.HeaderService;

@Controller
public class HeaderController {

	@Autowired
	public HeaderService headerService;

	@GetMapping("/")
	public String index() {
		return "root.mid_contentList";
	}

	@GetMapping("/joinForm")
	public String GetjoinForm() {
		return "root.mid_joinForm";
	}

	@PostMapping("/joinForm")
	public void PostjoinForm(@RequestBody UserDto user) {
		headerService.join(user);
	}
	
	
	
}
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();
    // });
  },

  save: function () {
    //alert("user의 save함수 호출됨");
    let data = {
      //id값으로 찾아서 값 가져옴
      userId: $("#userId").val(),
      userName: $("#userName").val(),
      password: $("#password").val(),
      role: $("#role").val(),
    };

    //console.log(data);

    // ajax호출시 default가 비동기 호출
    // ajax 통신을 이용해서 3개의 데이터를 json으로 변경하여 insert요청!
    // ajax가 통신을 성공하고 서버가 json을 리턴해주면 자동으로 자바 오브젝트로 변환해주네요.
    $.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));
      }); //ajax 통신을 이용해서 3개의 데이터를 json으로 변경히여 insert 요청!!
  },

  update: function () {
    let data = {
      id: $("#id").val(),
      username: $("#password").val(),
      password: $("#password").val(),
      email: $("#email").val(),
    };
    $.ajax({
      type: "PUT",
      url: "/user",
      data: JSON.stringify(data),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
    })
      .done(function (resp) {
        alert("회원수정이 완료되었습니다.");
        location.href = "/";
      })
      .fail(function (error) {
        alert(JSON.stringify(error));
      });
  },

  // login: function () {
  //   //alert("user의 save함수 호출됨");
  //   let data = {
  //     //id값으로 찾아서 값 가져옴
  //     username: $("#username").val(),
  //     password: $("#password").val(),
  //     email: $("#email").val(),
  //   };

  //   //console.log(data);

  //   // ajax호출시 default가 비동기 호출
  //   // ajax 통신을 이용해서 3개의 데이터를 json으로 변경하여 insert요청!
  //   // ajax가 통신을 성공하고 서버가 json을 리턴해주면 자동으로 자바 오브젝트로 변환해주네요.
  //   $.ajax({
  //     //회원가입 수행 요청
  //     type: "POST",
  //     url: "/api/user/login",
  //     data: JSON.stringify(data),
  //     contentType: "application/json; charset=utf-8", //body데이터가 어떤 타입인지(MIME)
  //     dataType: "json", // 요청을 서버로 해서 응답이 왔을 때 기본적으로 모든 것이 문자열(생긴게 json이라면)=> javascript 오브젝트로 변경
  //   })
  //     .done(function (resp) {
  //       alert("로그인이 완료되었습니다.");

  //       location.href = "/";
  //     })
  //     .fail(function (error) {
  //       alert(JSON.stringify(error));
  //     }); //ajax 통신을 이용해서 3개의 데이터를 json으로 변경히여 insert 요청!!
  // },
};

index.init();

https://coco-log.tistory.com/105

 

Ajax통신할 때 리턴타입이 void인 경우

{"readyState":4,"responseText":"","status":200,"statusText":"parsererror"} 간단한 게시판을 만드는데, 삭제 버튼 구현 중에, 자꾸 위와 같은 에러 메세지가 났다. 상태코드가 200인데 parse에러? 문제는 컨..

coco-log.tistory.com

 

 

 

728x90

댓글