【发布时间】:2012-11-24 17:34:42
【问题描述】:
我正在使用 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>
【问题讨论】:
-
试试内容类型application/json
-
好的,我已经解决了我的问题,在 php 中: echo $_GET["callback"] 。 "(" . json_encode($arr) . ")";在 html 中: $.getJSON('array.php?callback=?', function(data) 这样就可以跨服务器了
标签: php javascript jquery getjson