Angular 2.0 Http client quick tip for firefox

14 בJune 2016

one comment

 

When getting HTTP response from a REST API using Angular 2.0 HTTP client you will
probably won’t mess around with your request headers.

You’ll probably count on the service to return a JSON formatted response,  subscribe
to this response and go ahead using it’s .json()  method which will use JSON.parse of
your browser API.

 

However, while this solution works fine using chrome,  Firefox behave differently. While in chrome,
the HTTP Accept headers will be */* , on Firefox it will put text/html,application/xhtml+xml
which basically will cause your server side to return its response in XML form.

 

Therefore what you need to do is change your Accept headers . You can do so by creating a Headers
type variable. You can do it using this small typescript code snippet below.

   1: let headers = new Headers({'Accept' : 'application/json'});

   2: let options  = new RequestOptions ({headers: headers});

   3:  

   4: //On Http.get - put the options variable as your second parameter

   5:  

   6: http.get('path/to/your/service', options);

 

Basically what we’ve done here is changing the ‘Accept’ header on our HTTP request and caused our
server side to return JSON.  Now, we are all set to work with Firefox as well.

 

Note: This quick tip is relevant to Angular 2.0 rc1. It may not be relevant in the future where Angular 2.0 is released.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. Pingback: Angular 2.0 Http client quick tip for firefox – AngularJS News