【问题标题】:Simulate http request [duplicate]模拟http请求[重复]
【发布时间】:2018-12-08 00:27:19
【问题描述】:

所以目前我正在使用 Angular 编写一个前端应用程序(不是很重要),并且我有一项服务目前提供硬编码的 json 数据。

import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Client } from '../models/client';

@Injectable({
  providedIn: 'root'
})
export class ClientService {

  clientsDataMock : Client[] = [    
    {name: "Client A", id: "15", profiles: [
      {name: "profile1"},
      {name: "profile2"},
      {name: "profile3"}
    ]},
    {name: "Client B", id: "20", profiles: [
      {name: "profileX"}
    ]},
    {name: "Client C", id: "25", profiles: [
      {name: "profileY"}
    ]}
  ];
  constructor() { }

  getClients(): Observable<Client[]> {
    return of(this.clientsDataMock);
  }

  getClient(id : String) : Observable<Client>{
    return of(this.clientsDataMock.find(client => client.id == id));
  }
}

目前 observable 并没有多大意义,在真正将所有这些连接到后端之前,我想从不同的文件加载所有数据并有点模拟 ajax 调用或 http 请求,这将在稍后实现。

有没有办法模拟这种行为并从单独的文件中获取数据?

【问题讨论】:

  • 您的问题不清楚。您可以像任何其他 AJAX 调用一样对服务器上的静态文件进行 AJAX 调用。你想做什么?
  • 我应该更清楚地说明问题。我从来没有进行过 ajax 调用或 hhtp 请求,但我可能想对静态文件进行 ajax 调用。 @SLaks
  • 对服务器端代码的 HTTP 请求和静态文件没有区别。

标签: javascript ajax angular http


【解决方案1】:

看看https://github.com/service-mocker/service-mocker 和其他类似的模拟 API 生成器。

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2018-05-29
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多