Javascript Uncaught (in promise) 使用 SweetAlert2 取消
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39321621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught (in promise) cancel using SweetAlert2
提问by Frankenmint
how do I properly escape the cancel button without throwing an error when using promises? My code throws an alert confirmation with a required checkbox. the code executes as it should to the user, but it throws an error in the console window:
使用 Promise 时如何正确转义取消按钮而不抛出错误?我的代码抛出一个带有必需复选框的警报确认。代码按照对用户的方式执行,但它在控制台窗口中引发错误:
Uncaught (in promise) cancel
Uncaught (in promise) 取消
//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) {
})
});
回答by Limon Monte
Update (Jan 2017):This issue has been fixed in v7: v7 upgrade guide ↗
更新(2017 年 1 月):此问题已在 v7 中修复:v7 升级指南 ↗
You need to add a rejection handler to the Promise. Alternatively, you can use .catch(swal.noop)
as a quick way to simply suppress the errors:
您需要向 Promise 添加拒绝处理程序。或者,您可以将其.catch(swal.noop)
用作简单地抑制错误的快速方法:
swal('...')
.catch(swal.noop);
PS. the package you're using is called SweetAlert2, not SweetAlert. In future questions please mention it so you can get more relevant answers.
附注。您使用的包称为 SweetAlert 2,而不是 SweetAlert。在以后的问题中,请提及它,以便您可以获得更相关的答案。
回答by Bergi
SweetAlert2 rejects the result promise when the cancel button is pressed. You can handle that:
当按下取消按钮时,SweetAlert2 拒绝结果承诺。你可以处理:
swal({
…
}).then(function(json_data) {
…
}, function(dismiss) {
if (dismiss === 'cancel') { // you might also handle 'close' or 'timer' if you used those
// ignore
} else {
throw dismiss;
}
})
If you don't need to do anything with the json_data
, you might also use the catch
method.
如果您不需要对 做任何事情json_data
,您也可以使用catch
方法。
回答by guest271314
new Promise(function(resolve, reject) {
is not necessary. $.post()
returns a jQuery promise object.
new Promise(function(resolve, reject) {
没有必要。$.post()
返回一个 jQuery 承诺对象。
Possible solution substitutes Promise.reject()
for new Promise()
constructor; removed .then()
that was placed as an option to first swal()
call; pattern appears to expect a Promise
to be returned from preConfirm
, though not certain what value is expected to be returned from .done()
other than json_data
.
可能的解决方案替代Promise.reject()
的new Promise()
构造函数; 删除.then()
了作为第一次swal()
调用的选项;模式似乎期望Promise
从返回preConfirm
,虽然不能肯定,预计将从返回什么值.done()
比其他json_data
。
swal({
title: "Confirm",
input: 'checkbox',
inputValue: 0,
type: "warning",
inputPlaceholder: 'I agree to <a href="#blahblahMore"></a>',
showCancelButton: true,
confirmButtonText: 'Confirm',
showLoaderOnConfirm: true,
preConfirm: function(result) {
if (result) {
return $.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
});
}
);
} else {
return Promise.reject('You must agree to our Terms & Conditions ');
}
},
allowOutsideClick: false
});
回答by kishore ms
Adding catch(swal.noop); at the end swal function will solve this problem
添加 catch(swal.noop); 最后 swal 函数将解决这个问题
For example:
例如:
swal({
}).then(function() {
}).catch(swal.noop);
回答by Adeojo Emmanuel IMM
you will need to catch the action for cancel
你需要捕捉取消的动作
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function(json_data) {
//delete item
}, function(dismiss) {
if (dismiss === 'cancel' || dismiss === 'close') {
// ignore
}
})