IE9(和其他 IE 版本)的 AJAX 问题

发布于 2024-11-03 12:49:57 字数 2814 浏览 0 评论 0原文

我在 IE9、IE8 和 IE7 中遇到 XMLHttpRequest 对象问题,尽管尚未在 IE7 中进行测试。它在 FF4、Opera 11.01 和 Chrome 10 中运行没有问题。 首先我想解释一下我使用这段代码的目的。我有一个 HTML 选择标签,其中定义了选项“时间”。然后,当用户单击按钮时,它会使用数据库中的时间值动态更新选择。下面是创建 XMLHttpRequest 对象的代码:

var xmlhttp = false;
if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
    }

如您所见,如果 XMLHttpRequest 对象创建失败,它会尝试创建 ActiveXObject。

现在是发送请求和获取响应的代码:

xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(time).innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "getTime.php?d=" + str, true);
      xmlhttp.send();

我将参数发送到 getTime.php,并将响应写回 id=time 的 select 标签。现在,在 IE9 和 IE8 中,它不想用数据库中的时间填充选择标记。

编辑: 我将从 getTime.php 添加代码:

<?php
$username="something";
$password="";
$database="somethingDB";

$date = $_GET["d"];
$timestamp = strtotime($date);
$nextDay= $timestamp + (1 * 24 * 60 * 60);// 7 days; 24 hours; 60 mins; 60secs
$date2 =  date('Y/n/j', $nextDay);


$link = mysql_connect('localhost', $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $link);
$query="SELECT TIME(Date) FROM someTable WHERE Date >= '" .$date. "' AND Date < '" .$date2. "'";
$result=mysql_query($query);
if (!$result) {
    die('Could not query:' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<option>" .$row[0]."</option>";
}

mysql_free_result($result);

mysql_close($link);
?>

EDIT2: 好的,我已经根据 this 添加了换行符。现在我还将发布代码选择标签,该标签现已包装:

<div id="wrap">
        <select id="Time1" name="Time1" disabled="disabled">
            <?php if (empty($_GET['Time1'])) { echo "<option>Ura</option>"; } else { echo '<option>' . $_GET['Time1'] . '<option>'; } ?>
        </select>
    </div>

getTime.php 中的代码现在也已更改,我将仅发布已更改的部分:

echo '<select id="Time1" name="Time1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<option>" .$row[0]."</option>";
}
echo '</select>';

如您所见,我仅添加了 echo '.... ' while 语句之前和之后。猜猜现在会发生什么。在 FF、Opera 和 Chrome 中,它可以正常工作,但在 IE9 中,它现在获取值,但不会将它们放入下拉菜单中,它只是将它们打印为实际文本。当值作为文本打印时,下拉菜单也会消失。似乎它不想包含选择标签。我不明白,为什么只有IE呢?

I am having problem with XMLHttpRequest object in IE9, IE8 and probably IE7 too, although have not tested in IE7. It works without problem in FF4, Opera 11.01 and Chrome 10.
First I would like to explain for what I use this code. I have a HTML select tag, with option Time defined in it. Then when the user clicks on a button, it dynamically updates select with time values from database. Now here is the code for creating XMLHttpRequest object:

var xmlhttp = false;
if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
    }

As you can see, if the creation of XMLHttpRequest object fails, it tries to create ActiveXObject.

Now the code for sending request and getting response:

xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(time).innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "getTime.php?d=" + str, true);
      xmlhttp.send();

I send the parameters to getTime.php, and the response is written back to select tag with id=time. Now in IE9 and IE8 it does not want to populate the select tag with time from DB.

EDIT:
I will add code from getTime.php:

<?php
$username="something";
$password="";
$database="somethingDB";

$date = $_GET["d"];
$timestamp = strtotime($date);
$nextDay= $timestamp + (1 * 24 * 60 * 60);// 7 days; 24 hours; 60 mins; 60secs
$date2 =  date('Y/n/j', $nextDay);


$link = mysql_connect('localhost', $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $link);
$query="SELECT TIME(Date) FROM someTable WHERE Date >= '" .$date. "' AND Date < '" .$date2. "'";
$result=mysql_query($query);
if (!$result) {
    die('Could not query:' . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<option>" .$row[0]."</option>";
}

mysql_free_result($result);

mysql_close($link);
?>

EDIT2:
Ok, I have added wrap, according to this. Now I will also post the code select tag, which is now wrapped:

<div id="wrap">
        <select id="Time1" name="Time1" disabled="disabled">
            <?php if (empty($_GET['Time1'])) { echo "<option>Ura</option>"; } else { echo '<option>' . $_GET['Time1'] . '<option>'; } ?>
        </select>
    </div>

The code in getTime.php is now also changed, I will post only the section which is changed:

echo '<select id="Time1" name="Time1">';
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo "<option>" .$row[0]."</option>";
}
echo '</select>';

As you can see, I only added echo '....' before and after while statment. Guess what happens now. In FF, Opera and Chrome it works without a problem, but in IE9, it now gets the values, but it does not put them in the drop down menu, it just prints them as actual text. Also the drop down menu has dissapeard when the values are printed as text. Seems like it does not want to include select tag. I don't get it, why is that only with IE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2024-11-10 12:49:57

我在此处找到了正确的解决方案。我已经在 div 框中添加了 select 标签,缺少的代码是这部分:

document.getElementById('box').style.display = 'block';

显然没有这个,IE 只会将其打印为实际文本,而不是填充 select 对象。

I have found the right solution here. I already had the select tag in the div box, the missing code was this part:

document.getElementById('box').style.display = 'block';

Apparently without this, the IE will just print it as actual text, instead of populating the select object.

一萌ing 2024-11-10 12:49:57

IE 的“选择”标签有问题。您不能只替换其中的“选项”标签,还必须替换开始和结束的“选择”标签。

IE has problem with 'select' tag. You cannot just replace the 'option' tags inside only, you have to replace the opening and closing 'select' tags also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文