Java 角度 4 httpclient xml 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45276250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular 4 httpclient xml response
提问by user2106630
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';
});
Hi I am using angular 4 httpclient to make a http get request from a spring controller which returns a XML response.
嗨,我正在使用 angular 4 httpclient 从返回 XML 响应的 spring 控制器发出 http get 请求。
The problem I have is the response is ALWAYS NULL even though I can see the xml response from the chrome developer network tab.
我遇到的问题是响应始终为 NULL,即使我可以从 chrome 开发人员网络选项卡看到 xml 响应。
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.
我认为这可能与请求标头有关,angular 4 默认为 json 但是我无法使用上面的代码更改请求标头。有人可以请指教。
Thanks
谢谢
回答by Karthikeyan Mohan
The issue here is the HttpHeaders are immutable in angular. So instead of setting the values, you should set when you create the object. Something like
这里的问题是 HttpHeaders 在角度上是不可变的。因此,您应该在创建对象时进行设置,而不是设置这些值。就像是
const headers = new HttpHeaders({ 'Content-Type': 'text/xml' }).set('Accept', 'text/xml');
But you are setting only request headers. If you want your response to be text/xml.
但是您只设置请求标头。如果您希望您的回复是 text/xml。
this.http.get('getxmlurl', { headers: headers, responseType: text/xml }).subscribe(response => { return response; });
You can remove headers unless you want to set request headers.
除非您想设置请求标头,否则您可以删除标头。
回答by Martin Schneider
Set the responseType
to text
:
设置responseType
为text
:
this.http.get('getXmlUrl', { responseType: 'text' }).subscribe(response => {
console.log(response);
});
Allowed values for responseType
:
的允许值responseType
:
arraybuffer
blob
json
(default)text
arraybuffer
blob
json
(默认)text
The responseType value determines how a successful response body will be parsed.
responseType 值确定如何解析成功的响应正文。
See Angular Docs:
HttpRequest # responseType
HttpClient # request()
请参阅 Angular 文档:
HttpRequest # responseType
HttpClient # request()
回答by Sql Surfer
2019 Angular 7 above HttpClient Note with Code
2019 Angular 7 以上 HttpClient Note with Code
Get Angular Response as Textor XmlNOT as Json
以文本或Xml而非 Json 的形式获取 Angular 响应
Some subtle changes may have occurred in Angular 7 after the other previous answers were written. This is like a detailed comment with code to MA-Maddin's answer.
在编写其他先前的答案之后,Angular 7 中可能发生了一些细微的变化。这就像对 MA-Maddin 的答案的详细注释和代码。
@Injectable()
export class MyWidgetService
private myHttpClient: HttpClient;
constructor(httpClient: HttpClient) {
super(httpClient); // you MIGHT need this
this.myHttpClient = httpClient;
}
getResAsObservableStr = () => {
// Override the JSON Default Behavior.
// 3 special things for text from HttpClient
// a: Calling the raw .get('url') NOT get<string>('url')
// b: override observe: 'body'
// c: override responseType: 'text'
return this.myHttpClient.get('pathUrlForCall'
, { observe: 'body', responseType: 'text'} );
}
// In Calling Coponent
export class MyWidgetComponent
valAsStr: string;
constructor(
// more vars like Router...
private myWidgetSvcRef: MyWidgetService) { }
ngOnInit() {
this.getMyStrValFromWeb();
} // ngOnInit end
getMyStrValFromWeb = () => {
this.myWidgetSvcRef.getResAsObservableStr()
.subscribe(valAsStr => {this.valAsStr = valAsStr; });
} // end getMyStrValFromWeb
// in your html template (one possible scenario)
<someControl [(ngModel)]="valAsStr" > </someControl>