【发布时间】:2022-01-04 00:51:01
【问题描述】:
我正在向 Controller 发送一个表单值,以通过 Springboot 项目中的 ajax 调用从数据库中加载更多数据。 但收到 404 xhr failed 错误(我也在使用 Spring 安全性)。
我已附上控制器、脚本和错误日志
@GetMapping("/company_selection")
public String showCompanies(Model model, @AuthenticationPrincipal CustomUserDetails user) {
model.addAttribute("pageTitle","Select Company");
model.addAttribute("companies",companyService.listCompanies(user.getId()));
model.addAttribute("CompanySelectCriteria", new CompanySelectCriteria());
Map<Long, String> years = new TreeMap<>();
model.addAttribute("years", years);
return "company_select";
}
@PostMapping(value = "/company_selection", produces = MediaType.APPLICATION_JSON_VALUE , consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Map<Long, String> showYears(Model model, @RequestBody YearCriteria yearCriteria) {
Map<Long, String> years = companyService.listYears(yearCriteria.getCompanyName());
return years;
}
JQuery Ajax
<script type="text/javascript">
$(document).ready(function() {
$("#companies").change(function() {
var company = $(this).find(":selected").val();
var json = {
"companyName" : company
};
$.ajax({
type : "POST",
contentType : "application/json",
url : "/company_selection",
data : JSON.stringify(json),
dataType : 'json',
cache : false,
timeout : 600000,
success : function(data) {
var html = '';
html += '<option value="0"></option>';
for (let elem of data.entries()) {
html += '<option value="' + '${elem[0]}' + '">'
+ '${elem[1]}'
+ '</option>';
}
html += '</option>';
$('#year_id').html(html);
},
error : function(e) {
alert(e);
}
});
});
});
</script>
【问题讨论】:
标签: java ajax spring-boot http-status-code-404