在Angular 7中,使用HttpClient从Laravel API获取响应可以通过以下步骤实现:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
this.http.get('https://api.example.com/data').subscribe(response => {
console.log(response);
});
this.http.get('https://api.example.com/data', { responseType: 'text' }).subscribe(response => {
console.log(response);
});
在上面的代码中,我们通过在get请求的配置中设置responseType为'text'来告诉HttpClient我们希望以纯文本形式获取响应。
请注意,如果Laravel API返回的是JSON数据,你可以将responseType设置为'json',这样HttpClient会自动将响应解析为JSON对象。