【发布时间】:2010-10-30 13:05:03
【问题描述】:
我想在test.php中创建一个hello world json rest webservice:
<?php header("Content-type: application/json; charset=utf-8");
$test[] = "hello";
$test[] = "world";
$json = json_encode($test);
echo $json;
?>
但是当我用下面的 ajax 测试它时什么都没有返回,为什么?
<html>
<head>
<script>
function test()
{
var xhr;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
catch (e)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3) { xhr = false; }
}
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
alert(xhr.responseText);
else
alert("Error code " + xhr.status);
}
};
xhr.open(GET, "test.php", true);
xhr.send(null);
}
</script>
</head>
<body>
<script>
test();
</script>
</body>
</html>
【问题讨论】:
-
为什么不使用 jQuery 或其他库?
-
因为我想先学基础(json + xmlhttprequest)
标签: php javascript ajax web-services json