<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript"></script>
</head>
<body>
<ul>
</ul>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET","users.php?id=2");
xhr.send();
xhr.onreadystatechange=function(){
if(this.readyState==4){
console.log(this.responseText);
}
}
</script>
</body>
</html>
//PHP代码
<?php
header('Content-Type: application/json');
$data = array(
array(
'id' => 1,
'name' => '张三',
'age' => 18
),
array(
'id' => 2,
'name' => '李四',
'age' => 20
),
array(
'id' => 3,
'name' => '二傻子',
'age' => 18
),
array(
'id' => 4,
'name' => '三愣子',
'age' => 19
)
);
if (empty($_GET['id'])) {
$json = json_encode($data);
echo $json;
}
else{
foreach ($data as $item) {
if ($item['id'] != $_GET['id']) continue;
$json = json_encode($item);
echo $json;
}
}
宣言的言
慕设计2395807
小吴同志