gpt4 book ai didi

java - 将数据从 Controller 传递到 Ajax Java Spring MVC 时出现错误 500

转载 作者:行者123 更新时间:2023-12-01 17:11:27 26 4
gpt4 key购买 nike

我想将数据从 Controller 收集到ajax中,并在单击按钮编辑时将其显示到模态JSP。但我收到错误 500。

模拟.jsp

<button class="btn btn-primary btn-edit" data-id="${simulasis.id}">Update</button>

Admin.js

$(document).ready(function () {
$('.table .btn-edit').on('click', function (event) {
event.preventDefault();
var id = {'id': $(this).attr('data-id')};
$.ajax({
url: '/backoffice/edit',
data: id,
contentType: "application/json",
dataType: "json",
type: 'GET',
success: function (response) {
$('#idEdit').val(response.id);
console.log("success");
},
error: function (jqxhr) {
console.log(this.url);
console.log(response);
}
});
console.log(id);
$('#edit-modal-lg').modal();
});
});

BackofficeController.java

 @RequestMapping(value = "/edit", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Simulasi getById(@RequestParam("id") int id){
return simulasiService.getSimulasi(id);
}

我遇到了这样的错误,

enter image description here

最佳答案

尝试以下解决方案,

以json格式声明数据如下,

data: {id : id},
//contentType: "application/json", (omit contentType)

getById方法返回纯java对象

@RequestMapping(value = "/edit", method = RequestMethod.GET)
@ResponseBody
public Simulasi getById(@RequestParam("id") int id){
return simulasiService.getSimulasi(id);
}

关于java - 将数据从 Controller 传递到 Ajax Java Spring MVC 时出现错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61427493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com