Php 如果没有firebug断点,jquery ajax将无法工作

Php 如果没有firebug断点,jquery ajax将无法工作,php,jquery,mysql,ajax,firebug,Php,Jquery,Mysql,Ajax,Firebug,我使用以下方法调用php: function validateEmaiAjax(email){ val = null; $("#warning").load("https://localhost/Continental%20Tourism/register_ajax.php",{email: email}, function(rspns, stat, xml){ val = rspns; }); if(val == ".") re

我使用以下方法调用php:

function validateEmaiAjax(email){
    val = null;
    $("#warning").load("https://localhost/Continental%20Tourism/register_ajax.php",{email: email}, function(rspns, stat, xml){
        val = rspns;
    });

    if(val == ".")
        return true;
    else {
        return false;
    }
}
我的php代码是:

<?php
    $dbc = mysqli_connect("localhost","root","pass","continental_tourism") OR die(mysqli_connect_error());

    $email = $_REQUEST['email'];

    $query = "SELECT email FROM customer_info WHERE email = '$email' ";

    $r = mysqli_query($dbc, $query) OR die(mysqli_error($dbc));

    if(mysqli_num_rows($r) > 0)
        echo "Email address exists!";
    else
        echo ".";   
?>
为什么会这样

else {
    return false;
    $("#warning").show();
}
$(“#警告”).show()将永远不会执行

编辑:好了:

function validateEmaiAjax(email){
    var URL     = 'https://localhost/Continental%20Tourism/register_ajax.php';
    var Args    = {email: email}

    $('#warning').load(URL, Args, function(html){
        if(html == '.'){
            return true;
        } else {
            $('#warning').show();
            return false;
        }
    });

    return false;
}
或者您也可以尝试以下方法:

function validateEmaiAjax(email){
    var URL     = 'https://localhost/Continental%20Tourism/register_ajax.php';
    var Args    = {email: email}

    $.ajax({
        url:     URL,
        type:    'GET'
        data:    Args,
        success: function(html){
            if(html == '.'){
                return true;
            } else {
                $('#warning').show();
                return false;
            }
        }
    });

    return false;
}

这是因为函数一直在运行,并在返回ajax响应之前点击
if(val==”)
。您需要在ajax回调函数中插入整个if语句

function validateEmaiAjax(email){
    var success;
    $("#warning").load("https://localhost/Continental%20Tourism/register_ajax.php",{email: email}, function(rspns, stat, xml){
        if(rspns == ".")
            success = true;
        else {
            $("#warning").show();
            success = false;
        }
    });
    return success;
}

还交换了警告显示和返回,因此它将执行

,如果要使代码正常工作,应使用$.ajax方法,而不是load,并将async设置为false,使其等待响应


出现此问题的原因是您执行了一个异步请求。这意味着在从服务器收到响应之前,将到达
if(rspns==”)
,并且结果将始终为
false

为了将此代码封装在函数中,返回布尔值且不需要回调函数(阻塞过程),您需要使用同步请求:

function validateEmaiAjax(email) {

  // This is the correct way to initialise a variable with no value in a function
  var val;

  // Make a synchronous HTTP request
  $.ajax({
    url: "https://localhost/Continental%20Tourism/register_ajax.php",
    async: false,
    data: {
      email: email
    },
    success: function(response) {
      // Update the DOM and send response data back to parent function
      $("#warning").html(response);
      val = response;
    }
  });

  // Now this will work
  if(val == ".") {
    return true;
  } else {
    $("#warning").show();
    return false;
  }

}

这仍然不能解决问题
validateEmaiAjax()
现在将返回
undefined
@idrumgood在AJAX响应实际设置变量之前,您仍在返回该变量。@idrumgood我按您所说的编辑代码。但还是不行。但是,如果我设置一个断点,它仍然可以工作。由于
.load
函数不能直接返回值,因此您可能需要稍微重新构造其他代码以进行调整。不过,我的代码将解决您在帖子中提出的问题。这篇文章很棒。谢谢你的解释。现在我明白了。再次感谢。
function validateEmaiAjax(email) {

  // This is the correct way to initialise a variable with no value in a function
  var val;

  // Make a synchronous HTTP request
  $.ajax({
    url: "https://localhost/Continental%20Tourism/register_ajax.php",
    async: false,
    data: {
      email: email
    },
    success: function(response) {
      // Update the DOM and send response data back to parent function
      $("#warning").html(response);
      val = response;
    }
  });

  // Now this will work
  if(val == ".") {
    return true;
  } else {
    $("#warning").show();
    return false;
  }

}