我正在尝试用Ajax制作搜索引擎。当我在搜索框上输入时,它无济于事。当我检查Elemen并打开控制台时,它说script.js:19 GET http://localhost/var/www/html/pendaftaran-siswa/NaN 404 (Not Found)
当我单击源时,说未能在第19行加载源。这是我的脚本:
var keyword = document.getElementById('keyword');
var searchButton = document.getElementById('search-button');
var container = document.getElementById('container');
keyword.addEventListener('keyup', function(e) {
const src = e.target.value
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if( xhr.readyState == 4 && xhr.status == 200) {
container.innerHTML = xhr.responseText;
}
}
xhr.open('GET', 'ajax/mahasiswa.php?keyword=' + keyword.value, true);
xhr.send();
});
有解决这个问题的解决方案吗?对不起,我的英语不好
更新,现在像这样了:
Fatal error: Uncaught Error: Call to undefined function query() in S:xampphtdocsvarwwwhtmlpendaftaran-siswaajaxmahasiswa.php:11 Stack trace: #0 {main} thrown in S:xampphtdocsvarwwwhtmlpendaftaran-siswaajaxmahasiswa.php on line 11
也许
xhr.open('GET', 'ajax/mahasiswa.php?keyword=' * keyword.value, true);
应该是
xhr.open('GET', 'ajax/mahasiswa.php?keyword=' + keyword.value, true);
进一步调试:
console.log('ajax/mahasiswa.php?keyword=', keyword.value);