Reputation: 11
HTML:
<html>
<head>
<title>AJAX</title>
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
<script src="testing.js" type="text/javascript"></script>
<script src="http://www.json.org/json2.js"></script>
</head>
<body>
<input type="button" value="btnText" id="btntext" />
<input type="button" value="btnJSON" id="btnjson" />
</body>
</html>
PHP:
<?
header('Content-type: application/json');
echo '{"id": 1, "name": "abc", "email": "[email protected]"}';
?>
JS:
$(document).ready (function () {
$("#btntext").click(function () {
$.get("testing.text", function (data) {
alert(data);
});
});
$("#btnjson").click(function () {
$.get("testing.php", function (data) {
alert(data);
});
});
});
When clicking on the button for text, the alert works just fine. When clicking on the json button, the alert shows 'header('Content-type: application/json'); echo '{"id": 1, "name": "abc", "email": "[email protected]"}'
My question is, if I click the json button, how can I alert just the email to show '[email protected]'?
Upvotes: 1
Views: 280
Reputation: 16456
You are runing this without a server to run the PHP, what is causing the code to appear and not interpret. Eg. executing the html file directly and not by an http url.
After solving this, you can call alert(data.email);
to show the e-mail.
Upvotes: 1