使用诺言时,如何正确地退出取消按钮而不会引发错误?我的代码会发出带有所需复选框的警报确认。该代码按应对用户执行的方式执行,但会在控制台窗口中引发错误:
没抓住(承诺)取消
//validation logic all passes...Now proceed to... else { //determine and parse Discounts var myLookup = document.getElementsByName("myLookup")[0].value; $.post( "findthem.php", {myLookup: myLookup }) .done(function(json_data){ var theResponse1 = $.parseJSON(json_data); myDiscountRate = theResponse1['ourDiscountFound']; }).then( function(callback){ priceRate = priceRate * (1 - (.01 * myDiscountRate)); newRate = priceRate.toFixed(2); } swal({ title: "Confirm", input: 'checkbox', inputValue: 0, type: "warning", inputPlaceholder: 'I agree to <a href="#blahblahMore"></a> Your new Rate is :'+newRate, showCancelButton: true, confirmButtonText: 'Confirm', showLoaderOnConfirm: true, preConfirm: function(result) { return new Promise(function(resolve, reject) { if (result) { $.post("my.php", { Data: data }) .done( function(json_data) { var data_array = $.parseJSON(json_data); var moreDetails = ''; var resulting = 'error'; var details = "Transaction Declined" if (data_array["trxApproved"] == true) { resulting = 'success'; details = "Confirmed" moreDetails = "<br>Approved<b>" + data_array["approved"] + "</b>" + "<br>Details Code: <b>" + data_array["detailsCode"] + "</b>"; } swal({ type: resulting, title: details, html: "<h1>Details: </h1>" + data_array["messagetext"] + moreDetails }); } ); resolve(); } else { reject('You must agree to our Terms & Conditions '); } }); }, allowOutsideClick: false }).then(function(json_data) { }) });
更新(2017年1月): 此问题已在v7中修复:v7升级指南↗
您需要向Promise添加拒绝处理程序。另外,您可以使用.catch(swal.noop)一种快速的方法来简单地排除错误:
.catch(swal.noop)
swal('...') .catch(swal.noop);
PS。您使用的软件包称为SweetAlert 2 ,而不是SweetAlert。在以后的问题中,请提及它,以便您获得更多相关的答案。