php - 为什么ajax检索函数后不能自动运行JS函数?

标签 php javascript jquery ajax

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("#button1").click(function(){
 if(document.getElementById("hidValid").value == "Correct Username & Password"){
             parent.result.location.href="user.php";
            window.location.href="welcomeuser.php";
         }
      });
    });
    </script>
    <script type="text/javascript">
    function validate_use_pass(username,password){
        var loginUserID = document.getElementById("loginUserID").value;

        var passUserID = document.getElementById("passUserID").value;

        if(loginUserID == ""){
            alert("Invalid Username/Password.");
        }else if(passUserID == ""){
            alert("Invalid Username/Password.");
        }
        if(loginUserID == "admin" && passUserID =="admin" ){
    parent.result.location.href="account.php";
    window.location.href="welcome.php";
        }else{
            var xmlhttp;
            /*if(username.length==0 || password.length==0){
                document.getElementById("validateText").value="Enter Username/Password";
        return;
    }*/
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("validateText").innerHTML=xmlhttp.responseText;
        document.getElementById("hidValid").value=xmlhttp.responseText;
        }
      }

    xmlhttp.open("GET","validateUsePass.php?username="+username+"&password="+password ,true);
    xmlhttp.send();
}

}

问题是我似乎无法在“xmlhttp.send();”之后运行函数。 因此,当我登录时,我需要按两次按钮才能运行 JS 函数。

第一次按下按钮将从 PHP 检索数据,然后第二次按下按钮将运行 JS 函数。

最佳答案

试试这个:

    $.ajax({
        type : 'GET',
        url : 'validateUsePass.php',
        data: {
            username:$('loginUserID').val(), 
            password:$('passUserID').val()
        },
        success : function(data){
            alert('success')
        },
        error  : function(data){
            alert('error')          
        }

    } );

关于php - 为什么ajax检索函数后不能自动运行JS函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996807/

相关文章:

jquery - jQuery 插件中的异常处理

php - 在 AngularJS 中使用 ngRepeat 的控制台出错

实时服务器上 XMLHttpRequest 中的 Javascript 变量重置为空

Javascript:如何在不刷新页面的情况下更改地址栏中的 URL?

javascript - 在我的 AngularJS 动画中,这个动画延迟来自哪里?

javascript - 如何使用函数回调来设置新创建的元素属性?

javascript - 如何从数组创建 jQuery 对象?

php - HTML 和 PHP,用文件中的行填充下拉列表

php - ffmpeg 转换为没有音频的 mp4

javascript - ajax的问题​​。如何将javascript变量转换为php变量?