响应性无价值返回错误

发布于 2025-02-01 12:46:15 字数 1034 浏览 5 评论 0原文

我有一个带有百里叶应用程序的春季启动

启动ajax调用该代码,

$.ajax({
    type: "post",
    url: "/subscriptionsavetestament",
    async: false,
    data: testamentJson,
    contentType: 'application/json',
    dataType: 'json',
    beforeSend: function(request) {
        request.setRequestHeader(header, token);
    },
    success: function(data){
       ....
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log("error: " + textStatus);
    }
});

该控制器被称为该代码,

public ResponseEntity subscriptionsavetestament(@RequestBody TestamentWizard testamentDocument){
    ...
   return new ResponseEntity(HttpStatus.OK); 
}

但我在javascript侧侧错误解析上遇到了一个错误...

如果我修改了我的控制器以

public ResponseEntity subscriptionsavetestament(@RequestBody TestamentWizard testamentDocument){
    ...
   return new ResponseEntity(true, HttpStatus.OK); 
}

适合使用,但是我不这样做了解为什么

服务器仅返回状态,没有身体中的任何内容,为什么必须如何进行AJAX调用?

编辑:

删除数据类型解决问题

I have a spring boot with thymeleaf application

An ajax call is made wit this code

$.ajax({
    type: "post",
    url: "/subscriptionsavetestament",
    async: false,
    data: testamentJson,
    contentType: 'application/json',
    dataType: 'json',
    beforeSend: function(request) {
        request.setRequestHeader(header, token);
    },
    success: function(data){
       ....
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log("error: " + textStatus);
    }
});

This controller is called

public ResponseEntity subscriptionsavetestament(@RequestBody TestamentWizard testamentDocument){
    ...
   return new ResponseEntity(HttpStatus.OK); 
}

But I get alway an error on the javascript side error parsing...

If I modify my controller for

public ResponseEntity subscriptionsavetestament(@RequestBody TestamentWizard testamentDocument){
    ...
   return new ResponseEntity(true, HttpStatus.OK); 
}

that work fine, but I don't understand why

how the ajax call must be done if the server return only the status, nothing in the body?

Edit:

Removing dataType fix the issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2025-02-08 12:46:15

您可以使用以下代码返回使用200个状态代码的空体,

@GetMapping("/empty")
public ResponseEntity<Void> empty() {
    return ResponseEntity.ok().build();
}

如果您不想发送200,那么更好的方法将直接使用nocontent这将提供HTTP 204

return ResponseEntity.noContent().build();

You can return empty body with 200 status code using following code

@GetMapping("/empty")
public ResponseEntity<Void> empty() {
    return ResponseEntity.ok().build();
}

If you do not want to send 200 then a better way would be directly use noContent this will give HTTP 204

return ResponseEntity.noContent().build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文