我正在尝试使用 ajax 在我的 html 页面上显示 php 文件的内容。
我有一个 html 文件,其中包含以下 ajax 代码:
get_ajax.html
<form action="">
First name: <input type="text" id="txt1" onblur="show users(this.value)">
</form>
<p>Username: <span id="txtHint"></span></p>
<script>
function showHint(str) {
var xhttp;
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
}
xhttp = newXMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "user.php?u="+str, true);
xhttp.send();
}
</script>
user.php
<?php
echo $_GET["u"];?>
它不会在我的 get_ajax.html 页面上显示用户名。
我的代码有问题吗?
最佳答案
首先检查user.php
是否存在并验证正确的路径,顺便问一下为什么不使用Jquery,它很简单又直接。
这是一个使用 jquery 的示例:
var str = 'something';
$.get('user.php',{u:str},function(serverResponse){
$("#txtHint").html(serverResponse); //this should add the value something to the DOM
});
关于javascript - Ajax 无法从 php 文件加载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253452/