Angular 2 - Http - correctly ignore empty results

I have quite a few REST endpoints that process the request and just return 200. I noticed that this is an error for matching the result with json() . If I try not to perform any mappings, I see a browser warning that it cannot parse XML. Since returning nothing is normal, I am curious how I should handle the response.

Here is a sample base code:

 await this.http.put("/api/some_url", data).toPromise(); 

Here is the warning that appears in Firefox:

 XML Parsing Error: no root element found Location: http://localhost/api/some_url Line Number 1, Column 1: 

If I try to match with json() , it is completely reset. If I do nothing or try to match with text() , I just get a warning.

In all cases, the response headers contain Content-Length: 0 , so I'm surprised Angular2 / Firefox is trying to parse something. I do not know if this warning came from Angular2 or Firefox. I tried this on Chrome and IE and there were no warnings. Therefore, I am wondering if this is specific to Firefox.

+3
angular2-services
source share
1 answer

When the content is not returned, for example. return Ok() (HTTP status code 200) in ASP.NET Core, the content type (naturally) is not specified, but Firefox accepts the response as XML and tries to parse an empty document, which leads to the following console error:

XML parsing error: no root element was found. Location ...

Job return NoContent(); (HTTP status code 204) makes Firefox happy (and also works for other browsers).

Problem posted in Bugzilla @Mozilla :

and the analysis failure will simply be logged in the web console.

+2
source

All Articles