Post Ajax function to return true or false

I'm trying to get an Ajax function to return true or false (it's a sort of validation function which is called from another function). So the other function needs to get a true or false value from this one so it would know what to do next

This is the function:

function validateLogin( uid, pass, i ){

        var uvalue = uid.value;
        var pvalue = pass.value;

        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }

        xmlhttp.open("POST","validateCheck.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

                if( xmlhttp.responseText == "Error" )
                    return false;
                else if( xmlhttp.responseText == "Yes" )
                    return a;
                else if( xmlhttp.responseText == 1 )
                        return false;               
                else if( xmlhttp.responseText == 2 )
                        return false;
                else if( xmlhttp.responseText == 3 )
                        return false;
                else if( xmlhttp.responseText == 4 )
                         return false;
                else if( xmlhttp.responseText == 5 )
                        return false;
                else
                    return true;
            }
          }

          xmlhttp.send("uid="+uvalue+"&pass="+pvalue);  
    }

Calling function is:

function validateForm(i){
   var uid = document.myform1.username;
   var pass = document.myform1.password;

   if( validateLogin(uid,pass,i) )
        alert(validateLogin(uid,pass,i));

}

But the function validateLogin is not returning anything to the calling function(validateForm).


use callback

function validateLogin(uid, pass, i, callback) {
    var uvalue = uid.value;
    var pvalue = pass.value;

    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("POST", "validateCheck.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange = function ()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {

            if (xmlhttp.responseText == "Error")
                callback(false);
            else if (xmlhttp.responseText == "Yes")
                callback(a);
            else if (xmlhttp.responseText >= 1 && xmlhttp.responseText <= 5)
                callback(true);
            else
                callback(true);
        }
    };

    xmlhttp.send("uid=" + uvalue + "&pass=" + pvalue);
}

and call your function this way

function validateForm(i){
   var uid = document.myform1.username;
   var pass = document.myform1.password;

    validateLogin(uid, pass, i, function (resp) {
        if (resp) {
            alert(resp);
        }
    });
}

using Jquery Deffered Object

function validateLogin(uid, pass, i) {
   var result = jQuery.Deferred();
   ...
    if (xmlhttp.responseText == "Error")
       result.value = false;
    else if (xmlhttp.responseText == "Yes")
       result.value = a;
   ...
   xmlhttp.send("uid=" + uvalue + "&pass=" + pvalue);
   return result.value;
}

This function will not alert any thing. As your function execution get completed and still your ajax process work in background and your function does not return any thing and the if condition will get failed.

If you wany any validation then you have to place validation in ajax resonse.


  • you can call it synchronously if you need the result for the next operations:
  • xmlhttp.open("POST","validateCheck.php",false);

  • give a callback to the asynchronous function to work with the result

  • have a look on the jQuery Deferred Object

  • set a global var or a html element to the return value

  • ...

    链接地址: http://www.djcxy.com/p/55312.html

    上一篇: Ajax调用了Web服务器,但没有执行回调函数

    下一篇: 发布Ajax函数返回true或false