我与json-server
一起工作,并且在json
对象中得到了类似的结果,我想要一个名为dates
的属性,这些属性具有数字属性。
我想通过json
请求日期名称在其post
对象中添加其属性;
这是json文件:
{
"02.10.2019": {
"301": {
"status": "free",
"price": 20000,
"checkIn": "28.09.2019",
"checkOut": "02.10.2019",
"overallPrice": 80000
},
"302": {
"status": "engaged",
"price": 20000,
"checkIn": "28.09.2019",
"checkOut": "02.10.2019",
"overallPrice": 80000
},
"303": {
"status": "dirty",
"price": 20000,
"checkIn": "28.09.2019",
"checkOut": "02.10.2019",
"overallPrice": 80000
},
"304": {
"status": "free",
"price": 20000,
"checkIn": "28.09.2019",
"checkOut": "02.10.2019",
"overallPrice": 80000
}
}
}
我只想添加带有日期名称的第二个属性,并且想通过功能性来做到这一点,该功能获取字符串日期,并通过httpClient
添加到json-server
。
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs';
import {PutNewDateService} from './put-new-date.service';
@Injectable({
providedIn: 'root'
})
export class FirstGetService {
constructor(private http: HttpClient, private dateNew: PutNewDateService) { }
url1 = 'http://localhost:3000/';
private customizing(url: string, date: string): string {
return url + date;
}
getDate(date: string): Observable<{}> {
return this.http.get(this.customizing(this.url1, date));
}
setDate(date: string): Observable<any> {
return this.setGoOn(date);
// return this.http.post(this.customizing(this.url1, date), this.dateNew.getEmptyRooms());
}
private setGoOn(date: string) {
return this.http.post(this.customizing(this.url1, ''), date);
}
setSomething(data: string): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http.post('http://localhost/', data, httpOptions);
}
}
答案 0 :(得分:0)
只需构建适当的有效负载-根据需要添加为妈妈属性-并将其发送。
setSomething(data: string): Observable<any> {
const payload={
somefield: somevalue,
anotherfield:anothervalue,
actualData:data,
}
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http.post('http://localhost/', payload, httpOptions);
}
现在,服务器必须接受json编码的消息才能正确处理此类消息,因为字面payload
内容将作为请求正文发送。