[英]Using Promise in Javascript to Call the Function JQuery Ajax
This is my function using Jquery ajax:这是我使用 Jquery ajax 的函数:
function submit_otp(){
var otp=jQuery('#otp').val();
return jQuery.ajax({
url:'../../../Code_Verification/check_otp.php',
type:'post',
data:'otp='+otp,
success:function(result){
var result = new Promis((resolve, rejevy) => {
if(result=='yesTrue'){
resolve('Success')
}
if(result=='yesFlase'){
reject('Failed')
}
})
}
})
}
this is my callback with promise:这是我的承诺回调:
submit_otp().then(() =>{
FrontendBook.updateConfirmFrame(); //my next page
}).fail((error)=>{
jQuery('#otp_error').html('Please enter your valid otp');
$('#button-next-3').attr('disable', true);
$('#button-next-3').css('cursor', 'not-allowed');
})
--> if the return if resolve in promise that's mean i can go to next page, . --> 如果返回如果在承诺中解决,则意味着我可以转到下一页,. But if the return is reject that's mean the button-next-3 will be blocked.但是,如果返回是拒绝,则意味着 button-next-3 将被阻止。 The checkout_otp.php file is using php code to connect with database and call the value input and value in Database. checkout_otp.php 文件使用php代码连接数据库,调用数据库中的值输入和值。 I want the logical in here.我想要这里的逻辑。 thanks for help!感谢帮助!
The code is little bit messed up, you have JavaScript promises and jQuery Deferred what you need is this:代码有点乱,你有 JavaScript promises 和 jQuery Deferred 你需要的是:
function submit_otp() {
return new Promise((resolve, reject) => {
var otp = jQuery('#otp').val();
jQuery.ajax({
url: '../../../Code_Verification/check_otp.php',
type: 'post',
data: 'otp=' + otp,
success: function(result) {
if (result=='yesTrue') {
resolve('Success')
}
if (result=='yesFlase') {
reject('Failed')
}
}
})
})
}
you will get real promise so to use this function use this code:您将获得真正的承诺,因此要使用此功能,请使用以下代码:
submit_otp().then(() => {
FrontendBook.updateConfirmFrame(); //my next page
}).catch((error) => { // catch instead of fail
jQuery('#otp_error').html('Please enter your valid otp');
$('#button-next-3').attr('disable', true);
$('#button-next-3').css('cursor', 'not-allowed');
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.