我想从php中检索json数据并使用ajax输出并进行测试。但它不起作用。如何从php中检索json数据
客户的index.php
<html>
<head>
<title>kakak</title>
</head>
<body>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json.php", true);
xmlhttp.send();
</script>
</body>
</html>
这是json.php已经有对象。
<?php
$myArray = ["name" => "john", "age" => 30, "city" => "Japan"];
$myObj = json_encode($myArray);
?>
我想检索对象名称,但它不输出任何东西。
@TheCodesee你是什么意思呢?对象我想要在json.php中检索。 –
@TheCodesee'{“name”:“john”,“age”:30,“city”:“Japan”}'这是对象。 –
所以这是你应该在js中收到的。 – Hossam