首页 > 解决方案 > “done()”函数在 JS 中没有触发

问题描述

我正在尝试使用 Thymeleaf 在 Spring Boot 中构建应用程序。我需要向服务器发布一些数据,为此我使用以下 Ajax 代码:

$(".info").on('click', function () {
        var POSTdata = {
            "day": $(this).attr("data-day"),
            "date": $(this).attr("data-date")
        };
        console.log(JSON.stringify(POSTdata));
        $.ajax({
            url: "/shop/select/date",
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(POSTdata),
            success: function(){
                console.log("suc");
            }
        }).done(function () {
            console.log("sss");
        }).fail(error => console.log(error))
    });

但是 done() 没有触发。相反,它打印了很多行(来自失败回调),它们是更大数组的一部分,其中有一个字段显示 responseText: {"request":ok}

这是我发布到的功能:

@PostMapping("/shop/select/box")
public ResponseEntity<String> selectBox(@RequestBody String boxID, HttpSession session){
    session.setAttribute("boxID", boxID);
    System.out.println(session.getAttribute("boxID"));
    return new ResponseEntity<>("boxID", HttpStatus.OK);
}

标签: ajaxspring-bootpost

解决方案


推荐阅读