HTML code:
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function get_json()
{
$.post('json.php', function(data) {
var resp = eval('(' + data + ')');
var msg = resp.fruits;
console.log(msg);
alert(msg.letter+': '+msg.name);
});
}
</script>
</head>
<body>
<div style="margin:10px;" align="left">
<a href="javascript:void(0);" onclick="get_json();">Get Json response</a>
</div>
</body>
PHP code:
<?php
echo json_encode(array('fruits'=>array('letter'=>'a', 'name'=>'apple')));
?>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function get_json()
{
$.post('json.php', function(data) {
var resp = eval('(' + data + ')');
var msg = resp.fruits;
console.log(msg);
alert(msg.letter+': '+msg.name);
});
}
</script>
</head>
<body>
<div style="margin:10px;" align="left">
<a href="javascript:void(0);" onclick="get_json();">Get Json response</a>
</div>
</body>
PHP code:
<?php
echo json_encode(array('fruits'=>array('letter'=>'a', 'name'=>'apple')));
?>
Comments
Post a Comment