0

我有一个 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?

4

1 回答 1

0

您的方法需要发送一个包含您要发送的信息的数组,这是我实现它的小改动:

swal({
    title: '',
    text: '',
    type: 'info',
    showCancelButton: true,
    closeOnConfirm: false,
    showLoaderOnConfirm: true
  }, function () {
    self.citiesService.send_cities([{self._city.id, self._city.name, self._city.country, self._city.value}]).subscribe(
      response => {
        swal({
          title: "",
          text: "",
          type: "success",
          timer: 1500,
          showConfirmButton: false
        });

于 2021-05-11T17:27:25.460 回答