[英]How to avoid same API Calls multiple times on Angular 4.?
It is seen that when I submit a form on Angular 4, it makes 2 calls for a single call. 可以看出,当我在Angular 4上提交表单时,它一次调用2次。 When I check the log it is came to know that it is hitted two times.
当我检查日志时,发现它被击中了两次。 How can I over come this.?
我怎么能克服这个? When I submit all forms it inserted two times the same form data
当我提交所有表单时,它会插入两次相同的表单数据
As Chrillewoodz mentioned it can be OPTIONS request. 正如Chrillewoodz所说,它可以是OPTIONS请求。 This is pre-flight request made by some browsers as a safety measure to ensure that the request being done is trusted by the server.
这是某些浏览器发出的飞行前请求,作为一种安全措施,以确保服务器信任正在执行的请求。 You can`t disable it.
您不能禁用它。
If it is not an OPTIONS request make sure you don`t have "click" event on a 'submit' button and "ngSubmit" on a form itself. 如果不是OPTIONS请求,请确保您在“提交”按钮上没有“单击”事件,在表单本身上没有“ ngSubmit”。 Like this:
像这样:
you can solve it by make a boolean variable apiCalled
and if it is false don't call your api otherwise call it 您可以通过创建布尔变量
apiCalled
来解决它,如果为false,请不要调用您的api,否则请调用它
@Injectable()
export default class myService {
apiCalled: boolean = false;
consturctor(private Http _http){
}
callApi() {
if(apiCall)
return;
else {
this.apiCall = true;
this._http.get("write url").subscripe(
res => {
}, err => {
}, ()=> {
this.apiCall = false;
}
)
}
}
}
and once he finished calling the api you will be able to call that api again 他完成调用api后,便可以再次调用该api
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.