我有一个 POST 方法路由“http://localhost:5000/cities”,它接受 json 数据,例如
[{ "id:" 1, "name": "LA", "country": "USA", "value": 10000 }]
在 Angular 中,发布数据的服务 (cities.service.ts)
export class CitiesService {
constructor() { }
send_cities(data): Observable<Result> {
return this.post(this.Url(), data);
}
}
我有一个使用此服务的组件 (cities.component.ts)
let city_code = this.form.value;
swal({
title: '',
text: '',
type: 'info',
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true
}, function () {
self.citiesService.send_cities(city_code, self._city.id, self._city.name, self._city.country, self._city.value).subscribe(
response => {
swal({
title: "",
text: "",
type: "success",
timer: 1500,
showConfirmButton: false
});
如示例所示,如何以 json 格式将数据发布到 api?