有个朋友说PHP不能返回JSON对象,作为.net的我认为应该是可以的,设置一下header 就行了。
果不然,google 一下,备忘如下:
<?php
$result = array('Name' => 'Alex Yu', 'Status' => true, 'Message' => 'sss');
$jsonstring = json_encode($result);
header('Content-Type: application/json');
echo $jsonstring;
?>
$result = array('Name' => 'Alex Yu', 'Status' => true, 'Message' => 'sss');
$jsonstring = json_encode($result);
header('Content-Type: application/json');
echo $jsonstring;
?>
测试代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$.ajax({
url:'testjson.php',
type:"POST",
data:'',
success:function(phpData){
alert('Name:' + phpData.Name + ' Status:' + phpData.Status + ' Message:' + phpData.Message )
}
});
</script>
</head>
<body>
</body>
</html>