Javascript jQuery POST 400在选择值时出错
我在一张表格上有一个邮寄电话 我正在使用Spring+Thymeleaf 我有两个选择,是这样做的Javascript jQuery POST 400在选择值时出错,javascript,jquery,Javascript,Jquery,我在一张表格上有一个邮寄电话 我正在使用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}"
<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>
$(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个选择中选择两个不同的对象,一切都会顺利进行,如果我选择相同的地址,我会从帖子中得到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}
400响应意味着“错误的请求”,可能来自/book/getPrice中拒绝数据的逻辑。/book/getPrice现在返回0。。。我不明白为什么它以某种方式工作,而不是以另一种方式工作。我想展示你的控制器。你不知道数据类型。