簡體   English   中英

Ionic / Angular http.post將被多次調用

[英]Ionic/Angular http.post will be called more than one time

我使用Ionic3和Angular 5,嘗試通過http.post從客戶端向服務器發送json數據。

但是出於未知的原因,當我通過單擊按鈕手動發送發帖請求時,發帖請求將每隔幾分鍾自動被調用一次……但是我不希望這樣。 客戶只能發送一個帖子請求。

請任何人解釋問題是什么?

----我的電話班

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);
  });
 }
}

----我的ConnectionService

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>

在此處輸入圖片說明

我不確定,但是您也可以創建一個變量,如果成功,則可以調用此變量,例如:

公共canelReq = this.yourService.unsubscribe();

然后當您成功時,只需調用此方法

succsess => this.cancelReq();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM