3

I'm trying to throw an exception from my Web API controller. The problem is that my JavaScript code doesn't get any data back.

throw new HttpResponseException(HttpStatusCode.NoContent);

In my jQuery ajax function I don't get anything back. I get undefined back from the ajax call. So basically what is the proper way of handling errors from the server?

1 Answer 1

1

I feel very stupid right now, i tried a diffrent StatusCode and now i actully got some data back. I assumed every statusCode woulde return some value. This status code literaly returns NoContent.

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

2 Comments

FYI: "No Content" is actually a 204 status code. Any status code in the 2XX range is considered successful by the client. Therefore, throwing an exception and returning NoContent is a bit of a contradiction. You are probably looking for either BadRequest or NotFound as your status code.
The HTTP status code 204 makes the GET verb act like the HEAD verb, meaning no data should be returned. You should return the HTTP status code 200 or some other 2xx status. And I agree with Darrel that throwing a 2xx status code as an exception does not look right semantically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.