I am sending a value through a jquery ajax call to my spring controller. I want it to send an object back to populate a form in an iziModal. Currently, it sends the value from the browser back to my controller, and runs through my method in my controller, but then I get stuck. For some reason I'm having issues sending the response back to my ajax success function. I am getting this parse error: SyntaxError: Unexpected token t in JSON at position 1556482
Here is my controller method:
@RequestMapping(value="/editCarrierAjax", method= RequestMethod.POST)
public @ResponseBody CarrierAppointment getCarrierDets (@RequestParam("data") String data, MasterCarrier masterCarrier, Model model) throws Exception{
CarrierAppointment carrierToEdit = carrierAppointmentRepository.findById(Long.parseLong(data));
model.addAttribute("carrierToEdit", carrierToEdit);
return carrierToEdit;
}
Ajax Call:
$('.trigger-edit-carrier').on('click', function(event){
var selectId = $(this).attr('value');
console.log(selectId);
var token = $("meta[name='_csrf']").attr("content");
console.log(token);
var header = "X-CSRF-TOKEN";
console.log(header);
$.ajax({
type: "POST",
url: "/editCarrierAjax",
data: {data:selectId},
dataType:"json",
cache: false,
timeout: 600000,
beforeSend: function(xhr) {
xhr.setRequestHeader(header, token);
console.log(header +", "+ token);
},
success: function(data, jqXHR){
console.log("success fn");
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
});
I've tried adding the Jackson library mentioned here Convert a object into JSON in REST service by Spring MVC
but it still throws an error. Any idea how to fix this error?