2015-02-06 67 views
0

我有一个单页订单表单。用户填写我们的表单,javascript验证所有内容,如果它通过了Ajax请求,则会点击服务器,并在点击提交时通过Stripe向客户收费。如何处理响应中的ajax错误。

如果收费成功,json将返回到ajax请求({success:true})。然后用户重定向到成功页面,或者如果卡充电时发生错误,则显示错误。

我试图处理(用户的请求命中服务器,用户成功收费,但响应用户收到错误(很可能是移动/不稳定连接超时错误)的(罕见)问题。我怎样才能防止用户被双重收费?以下是我的ajax请求,但也许我需要重新考虑我的整个基础架构?

$.ajax({ type : 'POST', 
url  : '/order', 
data  : $(':input').serialize(), 
timeout : 30000, 
dataType : 'json', 
success : function (data) { 

    // redirect user to success page 
    window.location = '/completed'; 

}, 
error: function(xhr,status,error) { 

    // report the error to user. 

} }); 
+0

不条纹发送确认电子邮件或东西吗? – Blazemonger 2015-02-06 22:46:48

+0

你是否在处理任何后端?如果你是错误重试,如果它失败。如果您没有处理该事务,可能会设置一个此AJAX发布的小型后端应用程序,将该帖子发送给Stripe,并处理该可能的错误。编辑:另外,你怎么知道这个错误正在发生? – 2015-02-06 22:48:54

+1

显示一条消息,指出发生超时并设置超时以再次请求数据。或者你在服务器端处理双重费用(假设你有每个请求的ID) – Fuzzyma 2015-02-06 22:54:31

回答

0

试试这个..

$.ajaxSetup({ 
type  : 'POST', 
url  : '/order', 
data  : $(':input').serialize(), 
timeout : 30000, 
dataType : 'json', 
success : function (data) { 

    // redirect user to success page 
    window.location = '/completed'; 

}, 
error: function(xhr,status,error) { 

    if (xhr.status == 408) {//For Timeout 
       alert("Sorry, Request timeout.!"); 
       window.location.href ="//specify a redirect url//"; 
      } 
      else { 
       alert("An error occurred: " + status + "nError: " + error); 
      } 

} });