angular - 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>
解决方案
我不确定,但你也可以创建一个变量,如果你会成功调用这个变量,例如:
public canelReq = this.yourService.unsubscribe();
然后当你获得成功时,只需调用此方法
succsess => this.cancelReq();
推荐阅读
- date - 在 Kendo HTML Grid 客户端模板中比较日期
- javascript - 如何从 Rest Web Service 过渡到 PHP?
- opencart - 管理员订单信息上的 Sku
- python - 卷积层 - 定义输出张量流
- oracle - PL SQL 代码 **ORACLE **
- javascript - 使用不带 JQUERY的箭头标记水平滚动 div 图像
- python - 在全局声明之前使用名称“余额”
- centos7 - vsftpd 不会启动 - 没有错误信息
- java - 类型不匹配:无法从元素类型 Object 转换为 List
- excel - 从工作表 1 引用工作表 2 中的单元格,但在工作表 1 中每次向下拖动时索引该行