简体   繁体   English

Spring 引导 Controller 不使用 Ajax POST 更新数据库

[英]Spring Boot Controller Not Updating Database With Ajax POST

So I am using jQuery to send a POST request whenever a value of a dropdown part of a form changes, everything works EXCEPT that the Ticket is not getting updated in the DB, when I reload the page, the old values appear again.所以我使用 jQuery 在表单下拉部分的值发生变化时发送 POST 请求,一切正常,除了票证没有在数据库中更新,当我重新加载页面时,旧值再次出现。 I already confirmed that it is getting the correct Status and Priority with LOGGER, but for some reason is not updating the database, and this method works service.updateTicket(existentTicket);我已经确认它正在使用 LOGGER 获得正确的状态和优先级,但由于某种原因没有更新数据库,并且此方法有效service.updateTicket(existentTicket); (confirmed by another @PostMapping that uses the same line of code and updates the DB without issue) (由另一个使用同一行代码并毫无问题地更新数据库的@PostMapping 确认)

Controller: Controller:

@PostMapping("/tickets/details/{id}")
@ResponseBody
public String updateTicketFromDetailsForm(@PathVariable Long id, @RequestParam Integer statusId, @RequestParam Integer priorityId) {
    TicketEntity existentTicket = service.findTicketById(id);

    existentTicket.setStatus(service.findStatusById(statusId));
    existentTicket.setPriority(service.findPriorityById(priorityId));

    LOGGER.warn(existentTicket.getStatus().getName());
    LOGGER.warn(existentTicket.getPriority().getName());

    service.updateTicket(existentTicket);

    return "Success";

}

Form:形式:

            <form class="list-group list-group-flush" id="ticketDetailsForm">
                <li th:text="'Ticket ID: ' + ${ticket.getId()}" class="list-group-item">Ticket ID</li>
                <li th:text="'Created At: ' + ${ticket.getCreatedAtFormatted()}" class="list-group-item">Created
                    At
                </li>
                <li class="list-group-item">
                    <label class="ms-0">Status: </label>
                    <select class="d-inline dropdown-menu bg-gradient-info text-white text-center p-1 ms-2"
                            id="statusSelect"
                            required>
                        <option class="text-body" value="" disabled>Please select</option>
                        <option class="text-body" th:each="status : ${statuses}" id="statusOption"
                                th:value="${status.getId()}"
                                th:text="${status.getName()}"></option>
                    </select>
                </li>
                <li class="list-group-item">
                    <label class="ms-0">Priority: </label>
                    <select
                            class="d-inline dropdown-menu bg-gradient-info text-white text-center p-1 ms-2"
                            id="prioritySelect" required>
                        <option class="text-body" value="" disabled>Please select</option>
                        <option class="text-body" th:each="prio : ${priorities}" id="priorityOption"
                                th:value="${prio.getId()}"
                                th:text="${prio.name}"></option>
                    </select>
                </li>
            </form>

Screnshot of the form表格截图

And this is the jQuery:这是 jQuery:

$("#ticketDetailsForm").on("change", function () {
    $.ajax({
        method: "POST",
        url: $(location).attr('pathname'),
        data: {
            "statusId": $("#statusSelect").val(),
            "priorityId": $("#prioritySelect").val()
        }
    })
});

Not sure what I'm doing wrong, any help would be appreciated:)不确定我做错了什么,任何帮助将不胜感激:)

I figured it out, I just missed th:object="${ticket}" in <form> y and th:field="*{status}" (priority one too) in <select> .我想通了,我只是错过了<form> y 中的th:object="${ticket}"<select>中的th:field="*{status}" (也是优先级)。 Controller and jQuery was actually working. Controller 和 jQuery 实际上在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM