我正在使用 php 数组测试 getJSON,但它不起作用,有人可以帮我检查一下吗?我觉得问题出在 php 代码上,因为当我使用 getJSON url https://graph.facebook.com/zombies 测试 html 代码时, 这是工作。
在我的 array.php 中
<?php
header("Content-type: text/javascript");
$arr = array(
"name" => "Tim",
"age" => "28" );
echo json_encode($arr);
?>
在我的 test.html 中:
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
</head>
<body>
<script type='text/javascript'>
$(document).ready(function() {
$.getJSON('array.php', function(data) {
if(data) {
document.write(data.age);
}
else {
alert('error');
}
});
});
</script>
</body>
</html>
最佳答案
改变
header("内容类型:文本/javascript");
至
header('Content-Type: application/json');
对于 JSONP
header('Content-Type: application/javascript');
关于php - Jquery getJSON php 数组到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539964/