I am trying to submit form to Spring controller using AJAX call. Despite trying different options, I always get 400 Bad Request "The request sent by the client was syntactically incorrect." response. This is the JavaScript code that I use to send data:
$('#addNewTransactionForm').submit(function(event) {
var transaction = new Object();
transaction.date = $('#date').val();
transaction.value = $('#value').val();
transaction.categoryID = $('#categoryID').val();
transaction.accountToID = $('#accountTo').val();
transaction.accountFromID = $('#accountFrom').val();
transaction.description = $('#description').val();
$.ajax({
url: "${pageContext.request.contextPath}/transactions/create.json",
data: JSON.stringify(transaction),
type: "POST",
contentType: "application/json; charset=utf-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
});
event.preventDefault();
});
This is the Spring controller method:
@RequestMapping(value = "/create", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody TransactionFO createTransaction(@RequestBody @Valid TransactionFO transaction, final Principal principal) {
logger.info("createTransaction");
String name = principal.getName();
return transactionService.addNewTransaction(transaction, name);
}
This is the bean code:
public class TransactionFO {
private Date date;
@Digits(integer=8, fraction=4)
private BigDecimal value;
@NotNull
private Integer categoryID;
private Integer accountToID;
private Integer accountFromID;
@Length(min=0, max=255)
private String description;
// getters and setters
}
I am confused because I followed this tutorial and the code appears to be identical to me. I have following Jackson configuration in my .xml Spring configuration file:
<beans:bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jacksonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
I have noticed that if I change TransactionFO type to String, the request passes correctly. However, this is not a desired behavior, in this case I will be forced to parse JSON string manually. Any help will be hugely appreciated, I lost 2 days already on this issue.
Data from Chrome console:
Request URL:http://localhost:8080/finager/transactions/create.json
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:127
Content-Type:application/json
Cookie:JSESSIONID=31B00AA068448DB235C26F023F60A0BD
DNT:1
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/finager/transactions
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/32.0.1700.77 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{date:Sun Feb 02 17:22:29 CET 2014, value:0, categoryID:100, accountToID:35,accountFromID:null,…}
accountFromID: null
accountToID: "35"
categoryID: "100"
date: "Sun Feb 02 17:22:29 CET 2014"
description: ""
value: "0"
Thank you very much in advance.
PS. What is the proper way to send validation info to user via AJAX in order to avoid reloading the page? I have to perform complicated validation that could not be handled by the Hibernate Validator annotations.