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

7
  • stackoverflow.com/a/36370061/5695162 Commented May 31, 2018 at 11:58
  • stackoverflow.com/questions/45276250/… Commented May 31, 2018 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 Commented May 31, 2018 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 Commented May 31, 2018 at 12:24
  • this._http.get(this._url, { responseType: ' text' }).subscribe(response => { console.log(response); }); Give this a try and temme what you see Commented May 31, 2018 at 12:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.