Angular 如何在向json服务器发送字符串时正确使用post请求
我使用json服务器,我得到了类似的东西,在json对象中,我希望有一个名为dates的属性,这是带有数字属性的对象。 我想通过post请求日期名称及其属性添加这个json对象; 以下是json文件:Angular 如何在向json服务器发送字符串时正确使用post请求,angular,json-server,Angular,Json Server,我使用json服务器,我得到了类似的东西,在json对象中,我希望有一个名为dates的属性,这是带有数字属性的对象。 我想通过post请求日期名称及其属性添加这个json对象; 以下是json文件: { "02.10.2019": { "301": { "status": "free", "price": 20000, "checkIn": "28.09.2019", "checkOut": "02.10.2019", "
{
"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
}
}
}
我只想添加第二个带有日期名称的属性,我想用funtion(它获取字符串date)来实现这一点,然后由httpClient添加到json服务器
从“@angular/core”导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从“rxjs”导入{Observable};
从“./put new date.service”导入{PutNewDateService};
@注射的{
providedIn:'根'
}
导出类FirstGetService{
构造函数私有http:HttpClient,私有dateNew:PutNewDateService{}
url1=http://localhost:3000/';
私有定制url:string,日期:string:string{
返回url+日期;
}
getDatedate:字符串:可观察{
返回this.http.getthis.custominingthis.url1,日期;
}
setDatedate:字符串:可观察{
返回此.setgoundate;
//返回this.http.posthis.custominingthis.url1,date,this.dateNew.getEmptyRooms;
}
私有setGoOndate:string{
返回this.http.posthis.custominingthis.url1,日期;
}
setSomethingdata:字符串:可观察{
常量httpOptions={
标题:新的HttpHeaders{
“内容类型”:“应用程序/json”
}
};
返回此.http.post'http://localhost/,数据,httpOptions;
}
}
只需构建适当的有效负载—根据需要添加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编码的消息才能正确地处理这样的消息,因为实际有效负载内容将作为请求主体发送