Ajax成功:{return false;}

时间:2012-05-26 14:36:05

标签: javascript jquery ajax

我想在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;
    }
});

这不起作用。有工作吗?

5 个答案:

答案 0 :(得分:11)

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

最好从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');
  }
}

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

只使用async false它可以正常工作我已经实现了那样只有

试试吧

$.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;
    }
});

它工作正常尝试一次

答案 3 :(得分:0)

<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;
    }

}

答案 4 :(得分:-1)

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

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;

}