应用程序发送POST请求两次

问题描述:

我使用此代码发送POST请求到我的服务器。出于某种原因,单击该按钮将发送两个POST请求。第一个发送为/register/username/password。第二个发送到/register。问题是,第二个请求总是返回我404。应用程序发送POST请求两次

我试图解决它与postCount条件,但它仍然发送两个请求。
看来,当在Windows或Android上使用chrome时,它忽略了它的404工作正常。问题是,当使用mac或iOS它引发我“无法POST /注册”(因为404)。

This is the link to the app in Heroku

举例双岗REQ日志:

2017-08-31T11:36:18.896946+00:00 app[web.1]: POST /register/O**R/pass 200 0.423 ms - 3 
2017-08-31T11:36:18.939530+00:00 app[web.1]: POST/404 0.770 ms - 140 

Req代码:

var postCount = 0; 
$("#rgstnBtn").click(function() { 
    var password = $("#password").val(); 
    var username = $("#username").val(); 
    if(postCount === 0) { 
     if (username && password) { 
      $.post("/register/" + username + "/" + password, function (data) { 
       postCount++; 
       // console.log("hi"); 
       var status = data; 
       if (data === '500') { 
        location.reload(); 
        alert("username already exists"); 
       } else { 
        window.parent.location = "/item" 
       } 
      }); 
     } else { 
      refresh(); 
      alert('please insert valid user name and password'); 
     } 
    } 
    function refresh() { 
     setTimeout(function() { 
      location.reload() 
     }, 10); 
    } 
}); 
+0

看起来你很喜欢触发页面重新加载。我怀疑另一个POST发生在页面加载的其他地方。 –

+0

您可以移动** $(“#rgstnBtn”)之外的刷新功能,单击**功能并重试。 – Naveen

+0

对不起,这没有帮助:( – 2bis

好球员,我仍然不知道什么是双张贴的原因,但我用这个OP和问题解决了:

$("#rgstnBtn").attr('disabled', 'disabled'); 

感谢大家