Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Http Get always returning XML in Firefox, should be JSON

Tags:

angular

I am trying to call a Web Api endpoint from Angular 2. In IE and Chrome it all works fine but in Firefox I get a json parse error. I think its returning XML instead of Json. I thought that setting the content-type would have fixed this. Does my code look correct? Any ideas?

    let _tileUrl = XXX;

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this._http.get(_tileUrl, options)
        .map((response: Response) => <ITile[]>response.json())
                 .catch(this.handleError);
like image 578
Ben Cameron Avatar asked Jan 06 '23 19:01

Ben Cameron


1 Answers

I think that you should use the Accept header instead:

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

return this._http.get(_tileUrl, options)
    .map((response: Response) => <ITile[]>response.json())
             .catch(this.handleError);

The Content-Type header describes the type of content you send. Accept the content you expect in the response...

like image 107
Thierry Templier Avatar answered Feb 13 '23 22:02

Thierry Templier