我正在为 Angular 2 使用 angular-in-memory-web-api。到目前为止,我只使用了 GET 调用并且它运行良好。
我要调用的 API 仅使用 POST 调用,因此我开始将 GET 调用重写为 POST 调用,但随后它们停止返回模拟数据。在下面的测试函数中,我想通过 id 将数据作为 TestResponse 对象获取:
postTest(id: string): Promise<TestResponse> {
return this.http
.post(this.testUrl, JSON.stringify({ testId: id }), { headers: this.headers })
.toPromise()
.then(response => response.json().data as TestResponse)
.catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)
和模拟数据:
let test = [
{ testId: 'e0d05d2b-3ec3-42ae-93bc-9937a665c4d6', missingData: 'qwerty', moreMissingData: 'asdfgh' },
{ testId: 'dccef969-b9cf-410a-9973-77549ec47777', missingData: 'qwerty', moreMissingData: 'asdfgh' },
{ testId: '20716fd7-1f50-4a12-af16-52c009bc42ab', missingData: 'qwerty', moreMissingData: 'asdfgh' }
];
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,这段代码将假设我想创建一些东西,因此将我的 testId 与 id: 1 (它甚至不遵循我的数据结构)一起弹回。
所以,我的问题是,如何通过 …