英文:
AJAX POST request with json data to a spring controller returns status 405
问题
我使用Spring Thymeleaf返回一个带有对象的页面作为Spring属性。然后进行AJAX POST请求到一个Spring控制器。不知何故,我得到了以下错误:
"有一个意外错误(类型=方法不允许,状态=405)。
请求方法'GET'不受支持"
这个错误很奇怪,因为我并没有执行任何GET请求。
这是第一个Spring控制器,然后设置Param对象并传递给视图。
@RequestMapping("/listing")
public String privateReport(Principal principal, HttpServletRequest request, Model model) throws Exception {
Param param = new Param();
param.setStartDate(startDate);
param.setEndDate(endDate);
model.addAttribute("params", param);
return "listing-report";
}
视图中包含一个数据表格,并通过将Param作为JSON对象进行AJAX请求以获取数据。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>Private Corporate Patient Listing Report</h2>
</div>
<div class="container" style="margin-top: 25px;">
<table id="casesTable" class="table table-striped table-bordered nowrap">
<thead>
<tr>
<th>Date</th>
<th>Case Number</th>
<th>Patient Name</th>
<th>Address</th>
<th>Status</th>
<th>Charge Amt</th>
<th>Clinic</th>
<th>Doctor</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
crossorigin="anonymous"></script>
<script th:inline="javascript">
var params = [[${params}]];
$('#reportsTable').DataTable({
"processing": true,
"serverSide": true,
"bLengthChange": false,
"bFilter": false,
"ajax": {
type : "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url : "/report/listing-data",
data: JSON.stringify(params),
dataSrc: ''
},
"columns": [
{"data": "caseStartDate"},
{"data": "caseNumber"},
{"data": "patientName"},
{"data": "address"}
]
});
</script>
</body>
</html>
Spring REST控制器返回数据:
@RequestMapping(value = "/report/listing-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity privateReportData(@RequestParam("page") int page,
@RequestParam("size") int size,
@RequestBody Param param) {
param.setPage(page);
param.setSize(size);
logger.info("Requesting table data " + param);
return ResponseEntity.ok(reportDataService.getView(param));
}
Param模型:
@Getter
@Setter
@ToString
public class Param extends CommonParams {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startDate;
}
CommonParams模型:
@Getter
@Setter
@ToString
public class CommonParams {
private int page;
private int size;
}
我在哪里可能出错了?提前感谢您的帮助。
英文:
I'm using spring-thymeleaf to return a page with an object as a spring attribute. And then making an AJAX POST request to a spring controller. Some how im getting
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
error which is strange im not doing any GET request.
This is the first spring controller and then setting the Param object and pass to the view.
@RequestMapping("/listing")
public String privateReport(Principal principal, HttpServletRequest request, Model model) throws Exception {
Param param = new Param();
param.setStartDate(startDate);
param.setEndDate(endDate);
model.addAttribute("params", param);
return "listing-report";
}
The view which includes a datatable and doing an AJAX post to get the data by passing the Param as a json object listing-report.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>Private Corporate Patient Listing Report</h2>
</div>
<div class="container" style="margin-top: 25px;">
<table id="casesTable" class="table table-striped table-bordered nowrap">
<thead>
<tr>
<th>Date</th>
<th>Case Number</th>
<th>Patient Name</th>
<th>Address</th>
<th>Status</th>
<th>Charge Amt</th>
<th>Clinic</th>
<th>Doctor</th>
<th>Diagnosis</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
crossorigin="anonymous"></script>
<script th:inline="javascript">
var params = [[${params}]];
$('#reportsTable').DataTable({
"processing": true,
"serverSide": true,
"bLengthChange": false,
"bFilter": false,
"ajax": {
type : "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url : "/report/listing-data",
data: JSON.stringify(params),
dataSrc: ''
},
"columns": [
{"data": "caseStartDate"},
{"data": "caseNumber"},
{"data": "patientName"},
{"data": "address"}
]
});
</script>
</body>
</html>
The spring REST controller that returns data :
@RequestMapping(value = "/report/listing-data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity privateReportData(@RequestParam("page") int page,
@RequestParam("size") int size,
@RequestBody Param param) {
param.setPage(page);
param.setSize(size);
logger.info("Requesting table data " + param);
return ResponseEntity.ok(reportDataService.getView(param));
}
The Param model :
@Getter
@Setter
@ToString
public class Param extends CommonParams {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate startDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
}
CommonParams model :
@Getter
@Setter
@ToString
public class CommonParams {
private int page;
private int size;
}
Where would i have gone possibly wrong? Thanks in advance
专注分享java语言的经验与见解,让所有开发者获益!
评论