0

in my app i am trying to load XML file in order to use his data. but i am facing an error in Chrome browser console:

{error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttp…,

service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ReadDataService {
  headers = new HttpHeaders({ 'Content-Type': 'text/xml' });
  xml: string;
  private _url: string = "../../assets/sf9_commandpacket.xml";

 constructor(private _http:HttpClient) {}

  getData(): Observable<string> {
  return this._http.get<string>(this._url, { headers: this.headers });
  }
 }
}

i see in the error, that he is trying to load json, why?


I tried to implement this solution:

this._http.get(this._url, { headers: this.headers, responseType: text / xml }).subscribe(response => { return response; })

but this appear: this appear

also tried to surrong with ': enter image description here

Vikas
  • 11,859
  • 7
  • 45
  • 69
E.Meir
  • 2,146
  • 7
  • 34
  • 52
  • https://stackoverflow.com/a/36370061/5695162 – Vikas May 31 '18 at 11:58
  • https://stackoverflow.com/questions/45276250/angular-4-httpclient-xml-response – Vikas May 31 '18 at 11:59
  • Content-type is a way to specify the media type of request being sent from the client to the server ,and `HttpClient.get()` by default returns JSON if you are requesting non-JSON data you need to use `responseType:'text'` option as mentioned in one of the link I have shared – Vikas May 31 '18 at 12:07
  • @Vikas, i already stumbled across those, but they didn't work for me. please see the question, i added in the bottom of the question – E.Meir May 31 '18 at 12:24
  • `this._http.get(this._url, { responseType: ' text' }).subscribe(response => { console.log(response); });` Give this a try and temme what you see – Vikas May 31 '18 at 12:48
  • i see the same thing that happen in the second image that i have attached – E.Meir May 31 '18 at 13:12
  • don't include header in the request – Vikas May 31 '18 at 13:18

0 Answers0