简体   繁体   中英

How to call return false in ajax requests page?

How to call return false; in ajax requests page ?

First, When i press submit button , It's will call return checkform(this);

And in function checkform ( form ) will use ajax post to page test.php

In test.php can i use return fales; for break form id="f1"

I tested but not work, How can i do that ?

index.php

<form method="post" action="" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);" id="f1">
<label>
    Your Username
</label>
<input type="text" name="username" id="username">
<br>
<label>
    Your Password
</label>
<input type="password" name="password" id="password">       
<br>
<input name="submit" type="submit" value="Sign in"/>
<span id="mySpan_username_password"></span>
</form>



<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
        $.ajax
        (
            {
                url: 'test.php',
                type: 'POST',
                data: $('#f2').serialize(),
                cache: false,
                success: function (data) {
                    $('#mySpan_username_password').html(data);
                }
            }
        )
}
</script>
<form id="f2">
    <input type="text" name="username_send_value" value="xx"/>  
    <input type="text" name="password_send_value" value="yy"/>  
</form>

test.php

<script>
return faalse;
</script>

You can return an error response to your ajax request eg 404 .

test.php

<?php
header('HTTP/1.0 404 Not Found');
?>

This happens because your checkform method shoots an ajax call and just move on. It doesn't wait for the ajax response, this is because of the default async behavior of ajax call.

In order to fix it, first you need to decide whether you want the ajax call async or not.

if async:false

   function checkform ( form )
   {
      var response=false;
      $.ajax({
        url: 'test.php',
        type: 'POST',
        async:false,//just add async:false here
        data: $('#f2').serialize(),
        cache: false,
        success: function (data) {
            $('#mySpan_username_password').html(data);
            response=true;
        }
    });
    return response;
 }

if async:true

pass callback as parameter and handle code in callback.

   function checkform ( form ,callback)
   {
      var response=false;
      $.ajax({
        url: 'test.php',
        type: 'POST',
        data: $('#f2').serialize(),
        cache: false,
        success: function (data) {
            $('#mySpan_username_password').html(data);
            callback(true);
        }
        error: function(data){
           callback(false);
        }
    });

 }

Method call:

 checkform(form,function(response){
 if(response===true)
 //....
 else
 //...
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM