Ionic从HTTP POST返回错误的响应

时间:2017-11-06 00:51:38

标签: angular http ionic-framework response

我有一个非常简单的API,可以通过电子邮件发出创建用户的请求,如果它已经存在,则会返回错误。 问题是这个API正常工作,但不是Ionic。当我使用全新用户向URL发出请求时,它会返回一个已存在的响应,但是当我看到数据库时,它实际上已创建

我相信发生了什么: Ionic它正在提出请求,在完成之后,再做另一个并向我展示最后的结果。

注意:我用这个应用程序,“超级模板”,我不知道它是否有这样做的错误,因为我从来没有遇到过这个问题。

User.ts

signup(accountInfo: any) {
this.api.post('create_user&name='+accountInfo.name+'&email=' + accountInfo.email + '&pass=' + Md5.hashStr (accountInfo.password),
  {},this.reqOpts).map((res) => res.json()).subscribe((resp)=>{
    console.log(resp);
  })

Api.ts

post(endpoint: string, body: any, reqOpts?: any) {
  return this.http.post(this.url + endpoint, reqOpts);

我已经尝试在几个地方添加.share(),但我没有成功。

1 个答案:

答案 0 :(得分:0)

你可以防止用布尔值发送多个请求。也许那会有所帮助。这也可以帮助您在发送请求时加载指标和其他可能需要的东西。

有时甚至用户可能会点击几次而您不需要发送多个请求,因此您只需检查请求是否正在进行中。

isRequestSent: boolean = false;

signup(accountInfo: any) {
    if(!this.isRequestSent) {
        this.isRequestSent = true;
        this.api.post('create_user&name='+accountInfo.name+'&email=' + accountInfo.email + '&pass=' + Md5.hashStr (accountInfo.password),{},this.reqOpts)
        .map((res) => res.json())
        .subscribe((resp)=>{
            console.log(resp);
            this.isRequestSent = false;
        }, (error: any) => {
            this.isRequestSent = false;
        });
  }
}
相关问题