0

I'm sending AJAX request to make simple CRUD happens on my project, asynchronously

I don't need any response from server, I just need to insert, update or delete data on database.

I also learned there must be a response to a request, so I tried some ways to fake it, but all of them didn't feel right.

  1. return meaningless value

Just return any value like boolean or empty String, and not using it

@PostMapping("/whatever") public @ResponseBody boolean something() { doSomething(); return true; } 
  1. return something, but not using it

Almost same as 1, since I'm not using it

@PostMapping("/whatever") public @ResponseBody MyObject something() { doSomething(); return new MyObject(); } 
  1. doesn't care about response, just get errors and don't mind it

Use catch or always with ajax request, a lot of errors on console, it really doesn't feel right

@PostMapping("/whatever") public void something() { doSomething(); } 
@PostMapping("/whatever") public String something() { doSomething(); return "/page/url"; } 

All of them works as I want, but it makes me thinking there could be a better way to write a code when I don't need any response.

Any advice on this?

2
  • 2
    Usually when you do an insert you need some sort of id generated and returned Commented Jul 10, 2019 at 15:03
  • There will be a response, because that's how HTTP works. What you put in that response is up to you and can be a simple HTTP 200 OK (however you provide that in Spring) Commented Jul 10, 2019 at 15:05

1 Answer 1

2

Add the @ResponseStatus(HttpStatus.NO_CONTENT) annotation to the controller route and then you do not need to return anything. Your prototype will be "public void doSomething()".

We use this for delete calls.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.