我使用json-server,我得到了类似的东西,在json object中,我想有一个名为dates的属性,它是具有number属性的对象。我想按post请求日期名称及其属性添加到此json对象中;下面是json-file:
{
"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
}
}
}我只想用date name添加第二个属性,我想用funtion来做这件事,它得到字符串date,然后按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);
}
}发布于 2019-10-08 01:00:10
只需构建适当的有效负载-根据需要添加为mamy属性-并发送它。
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内容将作为请求正文发送。
https://stackoverflow.com/questions/58260229
复制相似问题