【问题标题】:Ajax call go to error but saving is ok on the server sideAjax 调用出错,但在服务器端可以保存
【发布时间】:2018-08-17 01:02:35
【问题描述】:

我使用弹簧靴 2。 我做了一个 ajax 调用来保存表单。

$("#factoriesForm").submit(function (e) {
    e.preventDefault();
    debugger;
    var factoriesId = $('#factoriesForm input[name="id"]').val();

    var form = transForm.serialize('#factoriesForm');
    form = JSON.stringify(form);

    $.ajax({
        type: "post",
        url: "/template/edit/factories/" + factoriesId,
        data: form,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            $('#factoriesForm input[name="id"]').val(data.id);

            new PNotify({
                title: /*[[#{success.title}]]*/ ,
                text: /*[[#{success.msg}]]*/ ,
                type: 'error',
                styling: 'bootstrap3',
                delay: 4000
            });

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

            new PNotify({
                title: /*[[#{error.title}]]*/ ,
                text: /*[[#{error.msg}]]*/ ,
                type: 'success',
                styling: 'bootstrap3',
                delay: 4000
            });

        }
    });

});

春天我的控制者会做

@PostMapping("template/edit/factories/{id}")
@ResponseBody
public ResponseEntity<FactoriesDto> edit(Model model, @RequestBody FactoriesDto dto, @PathVariable("id") Integer id) {
    return new ResponseEntity<>(factoriesFacace.save(dto), HttpStatus.OK);
}

保存已正确完成,但是当我在 js 中调试时,错误部分中的代码而不是成功。

JSON 输入意外结束”堆栈:“SyntaxError: Unexpected end of JSON 输入↵ at parse ()↵ at Ut (http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.min.js:2:73768)↵ 在 k (http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.min.js:2:77335)↵ 在 XMLHttpRequest。 (http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.min.js:2:79907)"

状态码是 200。

【问题讨论】:

    标签: jquery ajax spring


    【解决方案1】:

    尝试更改您的 Spring 控制器:

    @PostMapping("template/edit/factories/{id}")
    @ResponseBody
    public ResponseEntity<FactoriesDto> edit(Model model, @RequestBody FactoriesDto dto, @PathVariable("id") Integer id) {
        factoriesFacace.save(dto);
        return new ResponseEntity<>("", HttpStatus.OK);
    }
    

    【讨论】:

    • 尝试返回一个 JsonObject 类型( return new ResponseEntity(entities, HttpStatus.OK); ),其中“entities”是一个 JSONObject 变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多