Gustavo Gustavo - 4 months ago 20
reST (reStructuredText) Question

How to correctly send a json to a RestController using ajax?

I am trying to send a json to an api, but I get this error:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'status': was expecting ('true', 'false' or 'null')


This is the javascript:

$("body").on('click','.btn-danger' ,(function (){
var id = $(this).val();
var data = {"status": "canceled",
"response": ""};
console.log(data);
$.ajax ({
type: 'PUT',
url: '/api/pendingrequests/' + id,
contentType: "application/json",
data: data,
dataType : "html"
}).done (function () {
$(this).hide();
});
}));


The controller:

@RequestMapping(value = "/api/pendingrequests/{requestId}", method = RequestMethod.PUT)
public ResponseEntity updateRequestStatus(@PathVariable Long requestId, @RequestBody HolidayRequestAction action) throws IOException {
return updateRequestStatusService.updateRequest(requestId, action);
}


And POJO which I am trying to receive from the request:

public class HolidayRequestAction {
private String status;
private String response;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getResponse() {
return response;
}

public void setResponse(String response) {
this.response = response;
}
}


Why do I get this Parsing Exception ? What could be the problem ?

Answer

Your question contains the answer: 'status': was expecting ('true', 'false' or 'null')

So you can not send other values then 'true', 'false' or 'null'

But in the request goes "status": "canceled"