我在寫一個網站來搜索我的python數據庫。非JavaScript版本完美運行。但是現在我想使用ajax,因此不必刷新頁面。即,一旦搜索按鈕被點擊,結果就會顯示出來。但它不起作用(我點擊按鈕,沒有任何反應)。爲什麼不???爲什麼我的ajax,javascript工作?
<script language="javascript">
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
function getData(){
if(xmlhttp) {
var obj = document.getElementById("search-results");
xmlhttp.open("GET","http://127.0.0.1:8000/search/?name=i&city=o",true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 &&
xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseXML;
}}
xmlhttp.send(null);
}}
</script>
</head><body>
<form id="search-form" action="" method="get">
<input type="text" name="name" id="name">
<input type="text" name="city" id="city">
<input type="button" value="Search" onclick = "getData()">
</form>
<div id="search-results"></div></body>
運行與螢火頁啓用(插件)與Firefox和張貼任何錯誤,當您按下 – Ben
您可能需要在事件處理中 – secretformula
感謝 我嘗試螢火而事實證明,沒有錯誤時返回false按鈕,它顯示。我試圖切換到假,但那也沒有工作 – Kbob