js部分
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
<script>
let xhr = new XMLHttpRequest();
xhr.open("GET","testJson.php","true");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
let myJsonStr = xhr.responseText;
let myJsonObj = JSON.parse(myJsonStr);
console.log(myJsonObj.name,myJsonObj.age);
}
}
xhr.send();
</script>
php部分
<?php
echo '{"name":"laowang","age":18}';
?>