在Angular中,要设置XMLHttpRequest的'responseType'属性,可以使用HttpResponse的构造函数来实现。示例如下:
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
@Component({
selector: 'app-sample',
templateUrl: './sample.component.html',
styleUrls: ['./sample.component.css']
})
export class SampleComponent implements OnInit {
constructor(private http: HttpClient) { }
ngOnInit() {
const url = 'https://jsonplaceholder.typicode.com/todos/1';
this.http.get(url, { responseType: 'text' })
.subscribe((response: HttpResponse) => {
console.log(response.body);
});
}
}
在上述示例中,我们使用了HttpClient的get方法,并在参数中设置了'responseType'为'text'。然后,在订阅HttpResponse时,我们可以通过response.body来访问返回的文本数据。
需要注意的是,使用HttpResponse的构造函数来设置'responseType'属性,可以帮助我们避免出现“Failed to set the 'responseType' property on 'XMLHttpRequest'”这样的错误。
上一篇:佑安医院挂号容易吗-佑安医院挂号难,起早贪黑与黄牛斗智斗勇,网上挂号系统还卡
下一篇:Angular中,是否可以在provides中使用可选的useClass,在我的情况下只在非开发模式下加载GlobalErrorHandler?