Angular2 - HTTP 200 treated as error
Finally found the answer. Ajax, when it's expecting a json, fires off the error callback after getting HTTP 200 with a malformed json (including an empty file). The fix was to replace all empty responses with {}.
To do this in a ASP.Net WebApi2 server, you need to use return Ok("{}");
instead of just return Ok();
at the end of the method being called.
An alternative solution (and most likely a better one) is to change the calling code - in particular, .map((res: Response) => res.json())
is the bit of code that failed - a conditional checking if res
is empty could be added before calling res.json()
.
I was having the same problem here. But in my case it was that I forgot to tell Angular which kind of response my endpoint was sending. For more information you can read the GreyBeardedGeek response at stackoverflow.