jquery ajax在没有firebug断点的情况下不工作 [英] jquery ajax dont work without firebug break point

查看:73
本文介绍了jquery ajax在没有firebug断点的情况下不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法来调用php:

I am using following method to call the 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 ".";   
?>

基本上这会检查数据库,如果有电子邮件显示电子邮件地址存在!如果不是我想返回true (所以我回显。并进行比较)。奇怪的是,如果我在附近使用firebug设置断点if if(val ==。)程序正常工作并返回true。如果我删除该断点函数总是返回false。我不明白为什么会这样。请帮忙!谢谢。

Basically this do check the database and if email exists shows "Email address exists!" if not I want to return true(so I echo "." and compare it). The weird thing is if i put a break point using firebug near if(val == ".") program works correctly and returns true. If I remove that break point function always return false. I cant understand why this happens. Please help! Thanks.

推荐答案

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

The reason you have this problem is because you have performed an asynchronous request. This means that the if(rspns == ".") will be reached before the response has been received from the server, and the result will always be false.

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

In order to wrap this code in a function the returns a boolean and does not require a callback function (a blocking procedure) you will need to use a synchronous request:

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

}

这篇关于jquery ajax在没有firebug断点的情况下不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆