javascript - 模拟http请求

标签 javascript ajax angular http

所以目前我正在使用 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));
  }
}

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

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

最佳答案

看看这个 https://github.com/service-mocker/service-mocker以及其他此类模拟 API 生成器。

关于javascript - 模拟http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091065/

相关文章:

javascript - 如何从 Javascript 中的 AtomPub XML HTTP 响应中提取值(适用于 Google Apps 管理设置 API)

javascript - execCommand ('copy' ) 在 Ajax/XHR 回调中不起作用?

javascript - 如何将 highcharts-more.src.js 添加到 angular2 项目?

javascript - CSS/jQuery 菜单在 IE7 中不起作用

javascript - 如何让我的 Javascript 事件处理程序只执行一次?

javascript - Velocity.js 在淡出时留下文本痕迹

angular - 使用 RxJS 的 Promise.all()

php - 在选择更改时更新表

javascript - symfony 2 等同于 symfony 1 中的 url_for() 函数

mysql - 发布 http ://localhost:4200/addTask 404 (Not Found) in Angular2 and node js but values are printing in console