0

我正在使用 PHP、AJAX 和 MYSQLI 的组合来加载用户输入内容的建议(在每次按键时触发加载)。这在 chrome、ie10、firefox、safari 等中运行良好。然后我在 IE9 上尝试了它,它只显示“No Suggestions”(如果没有找到匹配项,我编写了代码来执行此操作)。我唯一的想法是这可能是我在谷歌上读到的缓存问题,但是在尝试所有建议时..我最终回到了我开始的地方..“没有建议”。所以,我很难过。这是我的 AJAX 的代码:

function activelyLoad(passedInfo, insertInto, urlInLib)
{
    if (passedInfo.length==0)
  { 
  document.getElementById(insertInto).innerHTML="";
  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(insertInto).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","../lib/"+urlInLib+"?q="+passedInfo,true);
xmlhttp.send();
}

这是我正在加载的 PHP 文件之一:

<?php
include('config.inc.php');
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Pragma: no-cache' );
$full = $_GET['q'];
$fullAr = explode(" ", $full);
$count = count($fullAr);
$var = $fullAr[0];
$query = "SELECT * FROM StudentInfo WHERE (StudentFirstName LIKE '%$var%' OR StudentPrefferedName LIKE '%$var%' OR StudentLastName LIKE '%$var%')";
$response = "";
if ($count >= 2) {
    $var2 = $fullAr[1];
    $query = "SELECT * FROM StudentInfo WHERE ((StudentFirstName LIKE '%$var%' OR StudentPrefferedName LIKE '%$var%') AND StudentLastName LIKE '%$var2%') OR ((StudentFirstName LIKE '%$var2%' OR StudentPrefferedName LIKE '%$var2%') AND StudentLastName LIKE '%$var%')";
}
$result = mysqli_query($connection, $query);
$num_results = mysqli_num_rows($result);
if ($num_results > 0) {
while ($row = mysqli_fetch_array($result)) {
    $id = $row['StudentID'];
    $functionList = "\"activelyLoad($id, 'detDiv', 'addtable.php'),showElem('#details')\"";
    $functionList2 = "\"logLoad($id),  showElem('#contactDets')\"";
    if(strcmp($row['StudentPrefferedName'], '') != 0){
    $response .= ("<tr><td>" . $row['StudentFirstName'] . " \"" . $row['StudentPrefferedName'] . "\" " . $row['StudentLastName'] . "</td><td><button type='button' onmousedown=" . $functionList . " onclick=" . $functionList2 . ">Get details</button></td></tr>");
    }
    else{
        $response .= ("<tr><td>" . $row['StudentFirstName'] . " " . $row['StudentLastName'] . "</td><td><button type='button' onmousedown=" . $functionList . " onclick=" . $functionList2 . ">Get details</button></td></tr>");
    }
}
}
else $response = "No suggestions";
echo($response);
mysqli_close($connection);
?>

以下是该页面在 IE9 中的相关 HTML 输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META HTTP-EQUIV=”Pragma” CONTENT=”no-cache”&gt;
<title>XXXXXXX</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="../lib/searchScripts.js"></script>
</head>
<body>
<h1>XXXXXXXX</h1>

<p>Please select one of the following options:</p>
<ul>
<li><a id="searchLink" href="#" onclick="showElem('#search')">Search for a student</a></li>

<div id="search">
<p><b>To find a student, start typing a name in the input field below:</b></p>
<form> 
Student Name: <input type="text" onkeyup="activelyLoad(this.value, 'txtHint', 'gethint.php'), showElem('#suggestions')">
</form>
<div id="suggestions"><h2>Suggestions:</h2> <div id="suggestionsBox"><table id="txtHint"><tr><td>No Suggestions</td></tr></table></div></div>

唯一让我认为它必须是缓存问题的是,它适用于所有现代最新的浏览器。任何帮助是极大的赞赏。

编辑IE9 的控制台返回

SCRIPT600: Invalid target element for this operation. for 'document.getElementById(insertInto).innerHTML=xmlhttp.responseText;'

这是我的问题吗?无效怎么办?如果此处出错,浏览器如何访问 AJAX 以加载“无建议”?

4

1 回答 1

0

Among many other issues you are trying to dump the AJAX results into a <table> element as the .innerHTML property.

Internet Explorer has a bug that won't let you set the content of a table tag via .innerHTML until the bug is fixed in IE10

Bug report: http://webbugtrack.blogspot.ca/2007/12/bug-210-no-innerhtml-support-on-tables.html

You would be better off to create a <div> and set the .innerHTML of that instead.

于 2013-07-10T20:15:33.603 回答