Javascript Ajax 成功:{return false;}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10767128/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:50:24  来源:igfitidea点击:

Ajax success: {return false;}

javascriptjqueryajax

提问by Rens Tillmann

I want to return falsefrom $.ajaxwhen successis complete:

我想false从完成$.ajax时返回success

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        return false;
    }
});

This doesn't work. Is there a work around?

这不起作用。有解决办法吗?

回答by thecodeparadox

From your post I guess that you call a function that contains the $.ajax()and try to return falseto that function. but you can't do that such way, because AJAXis asynchronous.

从您的帖子中,我猜您调用了一个包含 的函数$.ajax()并尝试调用return false该函数。但你不能这样做,因为AJAX是异步的。

It's better to call a function from the ajax success function like following:

最好从 ajax 成功函数中调用一个函数,如下所示:

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        var returned = true;
        if(some_condition_not_satisfied) {
          returned = false;
        } else {

        }
        // call a function
        calledFromAjaxSuccess(returned);
    }
});

function calledFromAjaxSuccess(result) {
  if(result) {
    alert('TRUE');
  } else {
    alert('FALSE');
  }
}

回答by Gusgus

Maybe you could try something like this (values 1 and 0 should be changed to the ones that you use):

也许您可以尝试这样的操作(值 1 和 0 应更改为您使用的值):

success: function(result){
if(result === '1'){
// do something
}
else
   return false;
}

回答by Raja Ram T

just use asunc false it can work fine i have implemented like that only

只需使用 asunc false 它就可以正常工作我只是这样实现的

just try it

就试一试吧

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
   async: false, //=>>>>>>>>>>> here >>>>>>>>>>>
    success: function(result) {
        return false;
    }
});

it is working fine try it once

它工作正常尝试一次

回答by harsha

<form action="yourpage" method="post" onsubmit="return matchpass();">
  <div>
   <label> Name</label>
   <input type="text" name="name" id="name">
  </div>
  <div>
   <label> Email ID</label>
   <input type="email" name="email" id="email">            
  </div>
  <div>
   <label> Mobile No</label>
   <input type="text" name="mob"  maxlength="10" onkeyup="check_if_exists();" autocomplete="off" id="mob">
  </div>                             
  <div>
   <button type="button" >Send</button>
  </div>
  <span id="err"></span>
  <div>
   <label> OTP</label>
   <input type="password" name="otp" id="otp"  maxlength="6" placeholder="****">
   <span id="err2"></span>
  </div>    
  <div>
   <input type="reset" value="Reset" class="reset-btn">
   <input type="submit" name="submit" id="submit" value="Submit" >              
  </div>
</form>
<input type="hidden"  id="otpcheck"/>



<script>
function matchpass()
{

     $.ajax({   
    type:"post",
    url: "yourpage",
    data:{ mobile:mob,otp:otp},
    success:function(data)
    {
       if(data==1)
       {
           document.getElementById("otpcheck").value=1; //important
       }else{

           document.getElementById("err2").style.color = "red";
           document.getElementById("err2").innerHTML = "invalid OTP Number ";
         document.getElementById("otpcheck").value=0; //important
       }


    }
    });

    if(document.getElementById("otpcheck").value==0){
        return false;
    }

}

回答by Jakir Hossen

I face this problem. then i found the actual solution. use async : false, inside ajax call

我面临这个问题。然后我找到了实际的解决方案。使用 async : false,在 ajax 调用中

function thisisavailavailusername(uname){
var dataString = 'username='+ uname.value;
var trueorfalse = false;
$.ajax({
    type: "post",
    url: "/BloodBook/checkavailableusername",
    data: dataString,
    cache: false,
    async : false, //user this then everything ok
    success: function(html){
        if(html=="true"){
            $('#validusername').html('Available');
            trueorfalse = true;
        }else{
            $('#validusername').html('Not Available');
            trueorfalse = false;
        }

    },
    error: function() {
        alert("Something Wrong...");
    }
});

return trueorfalse;

}

}