这个问题通常是由于后端 API 结构不一致或跨域问题导致的。可以采取以下方法进行解决:
确保后端 API 结构与前端代码中使用的结构相匹配,如果不匹配,可能会导致加载数据时出现故障。
可以在后端 API 上进行跨域设置,允许前端代码从不同的域加载数据:
// Install CORS package npm install --save cors
// Use it in the API const cors = require('cors') app.use(cors())
// 安装 Allow CORS: Access-Control-Allow-Origin 插件
// 在 app.module.ts 中开启跨域 import { HttpClientModule } from '@angular/common/http';
@NgModule({ imports: [ HttpClientModule ], ... }) export class AppModule {}
//在 service 中使用 withCredentials: true
makeRequest(): Observable
以上方法中的一种应该可以解决 Angular - Sample Down failed to load response data 问题。