我正在通过jQuery的ajax调用将一个值发送到我的Spring控制器。我希望它能够发送一个对象回来,以填充iziModal中的表单。目前,它会将值从浏览器发送回我的控制器,并在我的控制器方法中运行,但是我卡住了。由于某种原因,我在将响应发送回我的ajax成功函数时遇到问题。我得到了这个解析错误:JSON中意外的记号t,位置在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调用:
$('.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);
}
});
});
我尝试添加了这里提到的Jackson库(将对象转换为Spring MVC中的JSON REST服务)
但仍然出现错误。有什么想法可以解决这个问题吗?
dataType: "json"
吗?你返回的是json响应吗? - SwaticarrierToEdit
转换为 JSON 格式,因为你的 AJAX 是接受 JSON 的。你能否更新你的代码?然后检查一下console.log(data);
显示了什么? - Swati