要创建一个返回JSON数组的HTTP GET请求,可以使用以下步骤:
以下是使用JavaScript和Angular框架的示例代码:
// 使用Angular框架的组件
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-example',
template: `
<div *ngFor="let item of data">
{{ item }}
</div>
`
})
export class ExampleComponent {
data: any[];
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get<any[]>('http://example.com/api/data')
.subscribe(response => {
this.data = response;
});
}
}
在上面的示例中,我们使用了Angular框架的HttpClient模块来发送GET请求,并使用ngFor指令循环渲染返回的JSON数组中的元素。
领取专属 10元无门槛券
手把手带您无忧上云