选择值上的jQuery POST 400错误

时间:2017-06-28 15:29:33

标签: javascript jquery

我在表单上有一个POST调用。

我正在使用Spring + Thymeleaf

我有两个选择,以这种方式制作

    <label class="col-sm-2 col-form-label" for="address"
th:text="#{reservation.startAddress }">Park</label>
<div class="col-sm-4 input-group">
  <select th:type="*{startAddress}" class="form-control" th:field="*{startAddress.addressId}" id="selectedStartAddress" th:disabled="${chosenPark} or ${startedReservation}">
      <option th:each="a : ${startAddresses }" th:value="${a.addressId }"
th:text="${a.addressName }">Opzione</option>
</select>
</div>
</div>

<div class="form-group row">

<label class="col-sm-2 col-form-label" for="endAddress"
th:text="#{reservation.startAddress }">Park</label>
<div class="col-sm-4 input-group">
    <select th:type="*{endAddress}" class="form-control"
th:field="*{endAddress.addressId}" id="selectedEndAddress">
         <option th:each="ad : ${endAddresses }" th:value="${ad.addressId }"
</select>
    </div>
        </div>

在每个对象中都有一个Address对象列表,一个名为startAddress,另一个名为endAddress(列表具有相同的元素)

现在,我需要以这种方式用jQuery调用POST

$(function() {
        $('#priceCalculation').click(function(event) {
            event.preventDefault(); // prevent this form from being submited
            var dati = new Object();

            var selectedStartAddress = new Object();
            selectedStartAddress.addressId = $("#selectedStartAddress option:selected").val();
            selectedStartAddress.addressName = $("#selectedStartAddress option:selected").text();
            dati.startAddress = selectedStartAddress;

            var selectedEndAddress = new Object();
            selectedEndAddress.addressId = $("#selectedEndAddress option:selected").val();
            selectedEndAddress.addressName = $("#selectedEndAddress option:selected").text();
            dati.endAddress = selectedEndAddress;
            dati.price = null;
            if($("#resId").val() != null ){
                dati.reservationId = $("#resId").val();
            }

            var json = JSON.stringify(dati);
            console.log(json);

            $.ajax({
                type: "POST",
                url: "/book/getPrice",
                data: json,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data){

                     var formattedPrice = data / 100;
                     $("#price").html('€ ' + formattedPrice.toFixed(2))
                     $("#priceDiv").removeClass("hidden");
                     $("#submitForm").removeClass("hidden");
                     $("#priceButton").hide();
                     $("#price_hidden").val(data);
                     $("#toDate").prop("disabled", true);
                     $("#fromDate").prop("disabled", true);
                     $("#form-signin select").prop("disabled", true);

                },

                    error: function(xhr, textStatus, error){
                          console.log(xhr.statusText);
                          console.log(textStatus);
                          console.log(error);
                 }
            });

        });
    });

问题是,如果我从2个选项中选择两个不同的对象,一切顺利,如果我选择相同的地址,我从POST得到400错误,但我不明白为什么......

例如,使用此json可行:

{"fromDate":"2017-06-29T06:00:00.000Z","toDate":"2017-06-29T16:00:00.000Z","startAddress":{"addressId":"1","addressName":"Address 1"},"endAddress":{"addressId":"2","addressName":"Address 2"},"price":null}

这一个没有

{"fromDate":"2017-06-29T06:00:00.000Z","toDate":"2017-06-29T16:00:00.000Z","startAddress":{"addressId":"1","addressName":"Address 1"},"endAddress":{"addressId":"1","addressName":"Address1"},"price":null}

0 个答案:

没有答案