简体   繁体   English

离子从HTTP POST返回错误的响应

[英]Ionic returning wrong response from HTTP POST

I have an extremely simple API, where it is possible to make a request to create a user by email, if it already existed, returns an error. 我有一个非常简单的API,可以通过电子邮件发出创建用户的请求(如果已经存在)返回错误。 The problem is that this API works normally, but not in Ionic. 问题在于此API正常运行,但不适用于Ionic。 When I make the request to the URL, with a completely new user, it returns a response that it already exists, but when I see the database, it actually was created 当我用一个全新的用户向URL发出请求时,它返回一个已经存在的响应,但是当我看到数据库时,它实际上是创建的

What I believe is happening: Ionic it is making a request, and after completing it, does another and shows me the result of the last. 我相信正在发生的事情: Ionic正在发出一个请求,完成后又执行另一个请求,并向我显示最后一个的结果。

Note: I used for this App, the "Super Template", I do not know if it can have a bug that is doing this, because I never had this problem. 注意:我在此应用程序中使用了“超级模板”,我不知道它是否可能存在执行此操作的错误,因为我从未遇到过此问题。

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

I already tried adding the .share() in several places, and I did not succeed. 我已经尝试在多个位置添加.share() ,但未成功。

Could you guard yourself against sending multiple requests with a boolean. 您能防止自己发送带有布尔值的多个请求吗? Maybe that would be of some help. 也许那会有所帮助。 That can also help you with loading indicators and other things you may need while sending the request. 这也可以帮助您加载指示符以及在发送请求时可能需要的其他东西。

Sometimes even the user may click a couple of times and you don't need to send multiple requests, so you can just check if a request is in progress. 有时甚至用户可能会单击两次,并且您不需要发送多个请求,因此您可以仅检查请求是否正在进行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM