xml文件加载显示json错误
在我的应用程序中,我试图加载xml文件加载显示json错误,xml,angular,http-headers,angular5,angular-httpclient,Xml,Angular,Http Headers,Angular5,Angular Httpclient,在我的应用程序中,我试图加载XML文件以使用他的数据。 但我在Chrome浏览器控制台中遇到了一个错误: {错误:SyntaxError:JSON中的意外标记{console.log(response);});尝试一下,告诉我你看到了什么 import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable }
XML
文件以使用他的数据。
但我在Chrome浏览器控制台中遇到了一个错误:
{错误:SyntaxError:JSON中的意外标记<位于XMLHttp的JSON.parse()处的位置0处
服务台
但这似乎是:
还尝试使用”
:
Content type是一种指定从客户端发送到服务器的请求的媒体类型的方法,以及
HttpClient.get()
默认情况下,如果您请求的是非JSON数据,则返回JSON,您需要使用responseType:'text'
选项,如我在其中一个链接中所述shared@Vikas,我已经偶然发现了这些,但它们对我不起作用。请看问题,我在问题底部添加了this.\u http.get(this.\u url,{responseType:'text'}).subscribe(response=>{console.log(response);});
尝试一下,告诉我你看到了什么
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 });
}
}
}
this._http.get(this._url, { headers: this.headers, responseType: text / xml }).subscribe(response => { return response; })