angular 4 httpclient xml响应 [英] angular 4 httpclient xml response
问题描述
const headers = new HttpHeaders({ 'Content-Type': 'text/xml' });
headers.append('Accept', 'text/xml');
headers.append('Content-Type', 'text/xml');
this.http.get('getxmlurl', {headers: headers}).subscribe(response => {
return '1234';
});
您好我使用angular 4 httpclient从spring控制器发出一个http get请求,它返回一个XML响应。
Hi I am using angular 4 httpclient to make a http get request from a spring controller which returns a XML response.
我遇到的问题是响应总是为空,即使我可以从chrome开发人员网络选项卡中看到xml响应。
The problem I have is the response is ALWAYS NULL even though I can see the xml response from the chrome developer network tab.
我认为它可能与请求标题有关,angular 4默认为json但是我无法使用上面的代码更改请求标头。有人可以请一些建议。
I thought it might be something to do with the request header, angular 4 defaults to json however I am unable to change the request header with the code above. Can someone please advice.
谢谢
推荐答案
设置 responseType
到 text
:
this.http.get('getXmlUrl', { responseType: 'text' }).subscribe(response => {
console.log(response);
});
的允许值responseType
:
-
arraybuffer
-
blob
-
json
(默认) -
text
arraybuffer
blob
json
(default)text
responseType值确定成功响应正文的方式将
解析。
The responseType value determines how a successful response body will be parsed.
请参阅Angular Docs:
HttpRequest #responseType
HttpClient#request()
See Angular Docs:
HttpRequest # responseType
HttpClient # request()
这篇关于angular 4 httpclient xml响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!