无法使用angular 8注入的http类接收XML响应
我想执行一个HTTP服务,并使用AngularV8获得XML响应 我尝试编写一个服务,然后将其用于我的组件中。 你可以找到我的角度代码:无法使用angular 8注入的http类接收XML响应,angular,https,http-headers,Angular,Https,Http Headers,我想执行一个HTTP服务,并使用AngularV8获得XML响应 我尝试编写一个服务,然后将其用于我的组件中。 你可以找到我的角度代码: getConsultationPointSOAP(wsname:string): Observable<any>{ return this.http.get<any[]>(this.baseUrl +":"+ PORT_NUM + url+wsname+'?wsdl', httpOptions).p
getConsultationPointSOAP(wsname:string): Observable<any>{
return this.http.get<any[]>(this.baseUrl +":"+ PORT_NUM +
url+wsname+'?wsdl',
httpOptions).pipe(catchError(this.handleError));
}
private handleError(err: HttpErrorResponse) {
console.log(err.message);
return Observable.throw(err.message);
}
当我执行代码时,我得到一个200 as web服务状态,并在我的browsernetwork选项卡上得到XML响应,但我无法在控制台中得到结果,它显示:error,and
分析my_URL时Http失败
我正在尝试多种解决方案,如果您知道如何解决此问题,或者是否有解析解决方案,我将不胜感激。我无法理解您的代码结构。请尝试更改下面的代码
this.soapservice.getConsultationPointSOAPWs(this._url).subscribe(
ngOnInit() {
response => {
this.soapservice = response.text;
console.log("OKEY");
},
error => {
console.log("error");
console.log(error.text);
}
);
}
到
尝试将httpOptions更改为responseType:text
getConsultationPointSOAPwsname:string:可观察{
return this.http.get<any[]>(this.baseUrl +":"+ PORT_NUM +
url+wsname+'?wsdl',
{responseType: 'text'}).pipe(catchError(this.handleError));
}
我希望这能让你锻炼。如果仍然不起作用,那么请确保在后端脚本中,您没有将头定义为-Content-Type:application/json 我找到了这个问题的解决方案,我必须在标题中添加我希望最终得到的响应类型:responseType:text,而不是httpHeaders,以防止获得原始格式JSON,相关代码如下:
getConsultationPointSOAPWs(url: string) {
this.resultat = this.http.get(url, {
headers: new HttpHeaders({
'Authorization': sessionStorage.getItem('token'),
'Content-Type': 'application/xml'
}),
responseType: 'text'
});
return this.resultat;
}
试试这个:-
ngOnInit() {
this.soapservice.getConsultationPointSOAPWs(this._url,{ responseType: 'text'}).subscribe(
response => {
this.soapservice = response;
console.log("OKEY");
},
error => {
console.log("error");
console.log(error.text);
}
);
}
Httpclient get方法有第二个参数作为选项,您可以在其中配置头。
在这种情况下,我们需要responsetype作为文本。因为默认response类型是json,这可能是您没有得到正确的xml类型响应的问题。如果解决方案适合您,请接受答案。通过单击我答案右侧的右键。在我的IDE中编写问题时出现了错误,这很好。仍然需要答案:
getConsultationPointSOAPWs(url: string) {
this.resultat = this.http.get(url, {
headers: new HttpHeaders({
'Authorization': sessionStorage.getItem('token'),
'Content-Type': 'application/xml'
}),
responseType: 'text'
});
return this.resultat;
}
ngOnInit() {
this.soapservice.getConsultationPointSOAPWs(this._url,{ responseType: 'text'}).subscribe(
response => {
this.soapservice = response;
console.log("OKEY");
},
error => {
console.log("error");
console.log(error.text);
}
);
}