0

嗨,我有一些 ajax 编码,其中 if 条件根本不起作用,每当程序执行 else 语句时,即使程序满足 if 语句也只能工作。

<script type="text/javascript">
    function CheckDetails()
    {
        var http = false;

        if (navigator.appName == "Microsoft Internet Explorer") {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            http = new XMLHttpRequest();
        }

        var rid = document.new_booking.ph_number.value;

        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                var str_d = http.responseText;
                if (str_d == "no") {
                    document.getElementById('cus_name').focus();
                } else {
                    var str_details = http.responseText;
                    var arr_details = str_details.split("~");
                    document.new_booking.cus_name.value = arr_details[0];
                    document.new_booking.pick_aline1.value = arr_details[1];
                    document.new_booking.pick_aline2.value = arr_details[2];
                    document.new_booking.pick_area.value = arr_details[3];
                    document.new_booking.pick_pincode.value = arr_details[4];
                    document.new_booking.drop_aline1.focus();
                }
            }
        }
        http.open("GET", "ajax.php?evnt=det&rid=" + rid);
        http.send();
    }
</script>

其 ajax.php 文件如下所示

<?php
    if ($_GET['evnt'] == 'det') {
        $rid = $_GET['rid'];
        include("configure.php");
        $select = mysql_query("select * from new_booking where ph_number = '$rid'");
        $count = mysql_num_rows($select);
        if ($count > 0) {
            $row = mysql_fetch_array($select);
            echo $row['cus_name']
                . "~" . $row['pick_aline1']
                . "~" . $row['pick_aline2']
                . "~" . $row['pick_area']
                . "~" . $row['pick_pincode']
                . "~" . $row['drop_aline1']
                . "~" . $row['drop_aline2']
                . "~" . $row['drop_area']
                . "~" . $row['drop_pincode'];
        } else {
            echo "no";
        }
    }
?>
4

3 回答 3

1

您可以使用 Chrome(或 Chromium)打开您的页面,然后使用内置调试器(Ctrl+Shift+I,“控制台”选项卡)调试您的 javascript 代码。我猜你会在那里看到一些 JS 错误。

基本上,您的代码工作正常(至少当我从中删除所有数据库访问时,因为我没有您的数据库)。

如果您不喜欢 Chrome,请使用 Firefox 和 FireBug 扩展。在“网络”页面上,您可以看到您的 ajax 请求已执行(或未执行)。

于 2013-09-19T17:10:13.453 回答
0

如果我不得不猜测,我会说一些空白正在某处潜入您的 AJAX 响应中。因为"no "不等于"no",所以它总是会碰到你的else分支。

您可能会考虑发回与空格无关的值。您可以重写整个混乱以使用 JSON,这将需要在两端的工作少得多:

// PHP:
if ($_GET['evnt'] == 'det') {
    $rid = $_GET['rid'];
    include("configure.php");
    $select = mysql_query("select * from new_booking where ph_number = '$rid'");
    $count = mysql_num_rows($select);
    if ($count > 0) {
        $row = mysql_fetch_array($select);
        // YOU PROBABLY WANT TO WHITELIST WHAT YOU PASS TO JSON_ENCODE
        echo json_encode($row);
    } else {
        echo json_encode([ "error" => "no" ]);
    }
}

// js:
http.onreadystatechange = function() {
    if (http.readyState == 4) {
        var str_d;
        try {
            str_d = JSON.parse(http.responseText);
        } catch(e) {
            str_d = false;
        }

        if (str_d && str_d.error === "no") {
            document.getElementById('cus_name').focus();
        } else {

            document.new_booking.cus_name.value = str_d.pick_aline1;
            document.new_booking.pick_aline1.value = str_d.pick_aline2;
            document.new_booking.pick_aline2.value = str_d.pick_area;
            // etc...
        }
    }
}
于 2013-09-19T17:24:27.663 回答
0

我遇到了类似的问题。这对我有用。

if (str_d.replace(/^\s+|\s+$/g, '') == "no")
于 2015-01-29T22:45:33.677 回答