首页 > 解决方案 > Ionic/Angular http.post 将被多次调用

问题描述

我使用 Ionic3 和 Angular 5 并尝试通过 http.post 将 json 数据从客户端发送到服务器。

但是由于未知的原因,当我每次点击按钮手动发送发布请求时,发布请求将每隔几分钟自动调用一次......但我不希望那样。客户端应该只发送一个发布请求。

请任何人都可以解释问题是什么?

----我的Calling课

import { ConnectionServiceProvider } from '../../providers/connection-service/connection-service';

@IonicPage()
@Component({
 selector: 'page-result-modal',
 templateUrl: 'result-modal.html',
 })

sendGameResults() {
 this.serverConnection.sendGameResult(result).subscribe(data => {
    console.log(result);
  }, error => {
    console.log(error);
  });
 }
}

----我的连接服务

import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core';

@Injectable()
export class ConnectionServiceProvider {
mainUrl = "http://127.0.0.1:8080";

constructor(public http: HttpClient) {}

sendGameResult(result) {
  var data = JSON.stringify(result);
  return this.http.post(this.mainUrl + '/sendgameresult', data, { headers: { 
     'Content-Type': 'application/json' } }).map(response => {
        return response;
  });
}

----- HTML

<ion-row nowrap no-padding>
        <button class="send-game-results" (click)="sendGameResults()" ion- 
    button full>Senden</button>
</ion-row>

在此处输入图像描述

标签: angularresthttppostionic-framework

解决方案


我不确定,但你也可以创建一个变量,如果你会成功调用这个变量,例如:

public canelReq = this.yourService.unsubscribe();

然后当你获得成功时,只需调用此方法

succsess => this.cancelReq();

推荐阅读