[英]Call server synchronously with AngularJS before form submit
我正在将我的AngularJS应用程序与一个支付网关集成,该网关需要提交一个表格以启动收集用户信用卡详细信息的过程。
流程如下:
为了使此过程正常进行,我需要先返回服务器调用,然后再从onsubmit返回true或false并允许提交表单。
所有使用promise($ q)的示例最终都会导致使用回调的代码。 如何通过同步调用实现上述流程,或者还有另一种方法可以实现我所需要的?
function allowSubmit() {
var scope = angular.element(document.querySelector( '#bookingForm' )).scope();
try {
var success = scope.createPayment();//createPayment is a method on my controller I need to synchronously call my backend with
return success;
} catch (e) {
//log/alert
return false;
}
}
<form id="bookingForm" name="bookingForm" novalidate action=" https://www.example.com/xyz/process.trans" method="POST" onsubmit="return allowSubmit();" >
...
<button type="submit" class="btn btn-success btn-lg pull-right">Payment</button>
...
</form>
解决方案看起来是:创建不带动作或方法的表单,然后设置它们并在控制器中的服务器上通过回调提交。
<form id="bookingForm" name="bookingForm" novalidate >
...
<button class="btn btn-success btn-lg pull-right" ng-click="createPaymentAndNavigate(bookingForm.$valid)">Payment</button>
...
</form>
//In server callback on controller
var bookingForm = document.getElementById('bookingForm')
bookingForm.action = "https://www.example.com/xyz/process.trans";
bookingForm.method = "POST";
bookingForm.submit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.