我在W3Schools上阅读了一个
AJAX教程,做了一个基本相同的简单例子,它产生了一个错误.如果好奇,教程就在这里:
http://www.w3schools.com/ajax/ajax_aspphp.asp
我想将一个字符串发送到PHP文件(类似于链接中的第三个代码块),但我收到一个错误:“找不到元素”. Firefox突出显示PHP文件的第5行,它只是?>.
带有AJAX代码的文件名为“getHello.html”(.html是否正确?)并且具有以下代码:
<!DOCTYPE html> <html lang="en"> <head> <Meta charset="utf-8"> <title>AJAX Hello World Example</title> <script> function sayHello(str) { if (str.length == 0) { document.getElementById("showtext").innerHTML = "Hello World!"; return; } var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { document.getElementById("showtext").innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET","sendHello.PHP?q="+str,true); xmlHttp.send(); } </script> </head> <body> <input type="text" id="textIn" onkeyup="sayHello(this.value);"/><br/><br/> <div id="showtext"></div> </body> </html>
PHP文件名为“sendHello.PHP”并具有以下代码:
<?PHP $q = $_GET["q"]; $response = "Hello World! Sent: ".$q; echo $response; ?>
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。