我的index.php文件:
<script>
var data;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
data = this.responseText;
console.log(data);
}
};
xmlhttp.open("GET", "getdata.php", true);
xmlhttp.send();
</script>
我的getdata.php文件:
<?php
require 'connectdb.inc.php';
$query = "SELECT `name` FROM `tasks` WHERE `id`='1'";
$query_run = mysql_query($query);
$name= mysql_result($query_run,0);
echo $name;
?>
我正在获取数据,但问题是它向我的数据添加了一个空的html文件结构,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>task1
您会发现'task1'写在最后,那就是我的数据
请帮助我,我之前也搜索过类似的其他问题,但我听不懂。谢谢。
答案 0 :(得分:-2)
尝试一下:
index.php:
$.ajax({
url: "getdata.php",
type: "POST",
success: function (result) {
var data = result.name;
console.log(data);
},
dataType: 'json'
});
getdata.php:
<?php
require 'connectdb.inc.php';
$query = "SELECT `name` FROM `tasks` WHERE `id`='1'";
$query_run = mysql_query($query);
$ajaxResponseArr["name"] = mysql_result($query_run,0);
exit(json_encode($ajaxResponseArr));
?>