본문 바로가기

SPRING

[SPRING]AJAX에서 객체 필드 보내기

클래스 안에 Object 필드를 잡았을 때, Ajax에서 값을 바인딩하는 방법

 

 

===========================================================================================

I'm trying to send a DTO object via AJAX to my controller, but i don't know how to send an Object which is a field of my DTO because i can only send the id of the object...

My DTO is

public class ReservationDTO {


    private Plate plate;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd/MM/yyyy HH:mm")
    @NotNull
    private Date fromDate;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd/MM/yyyy HH:mm")
    @NotNull
    private Date toDate;

    private Park park;

    //getters and setters 

 

answer:

 

$(function() {
            $
('#form-signin').submit(function(event) {
               
event.preventDefault();
           
var dati = new Object();
            dati
.fromDate = $("#fromDate").val();
            dati
.toDate = $("#toDate").val();
               
var selectedPlate = new Object();
                selectedPlate
.id = $("#selectedPlate").val();
                selectedPlate
.name = $("#selectedPlate option:selected").text();
                dati
.plate = selectedPlate;
            dati
.park = null;
                $
.ajax({
                    type
: "POST",
                    url
: "/book/getPrice",
                    data
: JSON.stringify(dati),
                    contentType
: "application/json; charset=utf-8",
                    dataType
: "json",
                    success
: function(data){
                         console
.log(data);
                   
}
               
});
               
return false;
           
});
       
});

 

출처: https://stackoverflow.com/questions/43632074/spring-and-ajax-form-how-to-send-an-object-field