我正在尝试使用Ajax来显示我的搜索结果,但它无法正常工作,我无法找到原因。这是我的.html文件,也包含Ajax。
JS
function showSearchResult() {
alert('beginning');
if (document.getElementById("search").value == "") {
document.getElementById("searchResult").innerHTML = "";
return;
}
try {
xhr = new XMLHttpRequest();
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// handle old browsers
if (xhr == null) {
alert("Ajax not supported by your browser!");
return;
}
var string = document.getElementById("search").value;
var url = "quote4.php?search=" + string;
xhr.onreadystatechange = handler;
xhr.open("GET", url, true);
xhr.send(null);
}
function handler() {
// only handle loaded requests
if (xhr.readyState == 4) {
if (xhr.status == 200) document.getElementById("searchResult").innerHTML = xhr.responseText;
else alert("Error with Ajax call!");
}
}
HTML
<h1>Live Search</h1>
<form>
<input id="search" size="100" type="search"> <input id="submit"
onclick="showSearchResult(); return false;" type="button" value="Go">
</form>
<div id="searchResult"></div>
PHP
<?
// pretend server is slow
// try to get quote
$handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?s={$_GET['search']}&f=e1l1hg", "r");
if ($handle !== FALSE)
{
$data = fgetcsv($handle);
if ($data !== FALSE && $data[0] == "N/A")
{
print("Price: {$data[1]}");
print("<br />");
print("High: {$data[2]}");
print("<br />");
print("Low: {$data[3]}");
}
fclose($handle);
}
?>
我无法找出问题所在,但是当我打开html文件并搜索麦克风的示例时,我不会得到任何结果,也不会发生任何事情。