javascript - Ajax 无法从 php 文件加载内容

标签 javascript php html ajax

我正在尝试使用 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/

相关文章:

javascript - 计算点坐标是否在具有凹 Angular 和凸 Angular 的多边形内? Javascript

mysql - PHP 处理 XML 数据

php - SQL 代码执行 : Trending topics equals a twitter

javascript - HowlerJS - 音频播放器 - 错误 401(未经授权)

html - flexbox 边距导致溢出

java - 带有javascript的Struts2参数标签

javascript - 我的 fullcalendar 插件中的弃用警告

php - 比较 SimpleXml 对象

javascript - 通过 Javascript 访问 HTML 表单元素

javascript - 长文本的省略号