Ajax提交问题-Rails [英] Ajax Submit issue - Rails
问题描述
我不认为我的解决方案可以通过Rails UJS提交braintree事务,而Ajax是正确的,原因是我在提交时看到两个ajax调用,而不是一个.
I am not convinced my solution to submit a braintree transaction via Rails UJS and Ajax is correct, the reason being is i am seeing two ajax calls as opposed to one when submitting.
例如,当关闭ajax(因此删除remote:true)时,提交表单似乎要等待现时才提交付款(我相信braintree会接管提交事件)
For example when turning ajax off (so removing remote: true) submitting a form seems to wait for a nonce before submitting the payment (I believe braintree takes over the submit event)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here", "first_name"=>"", "last_name"=>"", "payment_method_nonce"=>"nonce_here_populated"}
但是当使用远程提交时:启用true的第一个呼叫是
But when submitting with remote: true enabled the first call is
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here=", "first_name"=>"", "last_name"=>""}
所以我读到我需要使用 onPaymentMethodRecieved
回调函数等待该随机数,将随机数附加到表单上,然后提交
So i read that i need to wait for that nonce using the onPaymentMethodRecieved
callback function, append the nonce to the form and then submit
braintree.setup(gon.client_token, 'dropin', {
container: 'dropin-container',
onPaymentMethodReceived: function (paymentMethod) {
$('#braintree-transaction-form').append("<input id='p_nonce' type='hidden' name='payment_method_nonce' value='" + paymentMethod.nonce + "'></input>");
$("#braintree-transaction-form input[type='submit']").submit();
}
});
这导致拨打了两个电话
1)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here=", "first_name"=>"", "last_name"=>""}
2)
Parameters: {"utf8"=>"✓", "authenticity_token"=>"token_here", "first_name"=>"", "last_name"=>"", "payment_method_nonce"=>"nonce_here_populated"}
唯一的问题是我的控制器中有这个,认为不正确
Only problem being is i have this in my controller which dont think is correct
def create
nonce = params[:payment_method_nonce]
render action: :new and return unless nonce # Is this the way to wait for the nonce
@result = Braintree::Transaction.sale(
amount: 2500,
payment_method_nonce: nonce,
customer: {
first_name: params[:first_name],
last_name: params[:last_name]
}
)
end
我担心的是我正在拨打两个电话以及我的控制器代码.
my concern i guess is the two calls being made and my controller code.
任何人都可以使用Braintree和Rails进行Ajax调用
Has anyone got any experience with Ajax calls with Braintree and Rails
谢谢
推荐答案
完整披露:我在Braintree工作.如果您还有其他疑问,请随时与支持.
您可能会遇到提交按钮默认行为的问题,即默认行为您可以尝试禁用.但是,这里没有任何东西可以认为是问题所在.最有可能是您的设置中的其他地方出现了问题.我真的建议您与您的完整代码示例联系 Braintree支持,以帮助他们解决您的问题.
You might be running into a problem with the default behavior of your submit button, which is something you could try disabling. However, there's nothing here to think that is the issue. Most likely there is a problem elsewhere in your setup. I would really recommend contacting Braintree support with your full code samples to help them walk through your issue.
这篇关于Ajax提交问题-Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!