I'm calling a WebAPI controller from jQuery AJAX to request an item be deleted via a REST API.
The WebAPI controller returns a 204 (No Content) response, which then invokes a second request (GET). For this example I expect both requests to receive a 204 (No Content) response.
When viewed in Chrome both responses are fully recognised.
However, Firefox can't seem to cope with a 204 if it contains no content.
To obtain both the Chrome (above) and Firefox (below) screenshots the same requests were sent to a WebAPI controller from both browsers. The screenshot below shows that Firefox only recognises the first 204 response.
The response to these two requests is created differently:
The first response uses...
return Request.CreateResponse(HttpStatusCode.NoContent);
...which returns an instance of HttpResponseMessage
.
The second response uses...
return new NoContentResult();
...which we've defined as an implementation of IHttpActionResult
...
public class NoContentResult : IHttpActionResult
{
private readonly HttpRequestMessage _request;
public NoContentResult(HttpRequestMessage request)
{
_request = request;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = _request.CreateResponse(HttpStatusCode.NoContent, new StringContent("{}"));
return Task.FromResult(response);
}
}
The new StringContent("{}")
on the 4th-from-last line is an attempt to provide some content to help Firefox. I've also tried new StringContent("null")
with the same result.
Also, if that second response is inspected in Firefox, the following error message is found:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
It seems that Firefox is trying to parse this problematic 204, but it apparently makes no attempt to parse the first 204 response.
Furthermore, the request type (DELETE vs. GET) doesn't seem to matter: if I return 204 as an IHttpActionResult
from the first call then Firefox still doesn't like it.
Firefox also displays the first response as containing 168 bytes and the second as containing 0 bytes even though it will happily display the headers (below). Firefox is clearly upset at something.
Can anyone explain why Firefox has a problem with a 204 response when delivered as an implementation of IHttpActionResult
but not when it's delivered as an HttpResponseMessage
?
[The reason that the first request returns an HttpResponseMessage
while the second returns an IHttpActionResult
is that the first method has been in place for a long time whereas the second method is only now being added. And as IHttpActionResult
is easier to test, we'd like to use that.]