最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax success: {return false;} - Stack Overflow

programmeradmin1浏览0评论

I want to return false from $.ajax when success is complete:

$.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?

I want to return false from $.ajax when success is complete:

$.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?

Share Improve this question edited Jun 26, 2015 at 15:44 Sumurai8 20.7k11 gold badges68 silver badges102 bronze badges asked May 26, 2012 at 14:36 Rens TillmannRens Tillmann 4742 gold badges7 silver badges21 bronze badges 5
  • You mean you want to return false from $.ajax when success is complete? – d_inevitable Commented May 26, 2012 at 14:37
  • Yes, between the success function there is a script thats checks several variables, and based on that i want to return false; – Rens Tillmann Commented May 26, 2012 at 14:38
  • 4 You need to research callbacks. There are a lot of questions about this. – Jivings Commented May 26, 2012 at 14:38
  • Please show us the code that is supposed to read the return value. That way it will be much easier to provide a more comprehensive answer. – d_inevitable Commented May 26, 2012 at 14:39
  • You are returning false to the caller of that function which is jQuery internals and they don't really use the return value for anything. – Esailija Commented May 26, 2012 at 14:49
Add a comment  | 

5 Answers 5

Reset to default 12

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

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

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

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

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

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

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

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

}

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

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;

}

发布评论

评论列表(0)

  1. 暂无评论