5454 <li><a href="<?php echo site_url ('api/example/users/format/csv ' );?> ">Users</a> - get it in CSV</li>
5555 <li><a href="<?php echo site_url ('api/example/user/id/1 ' );?> ">User #1</a> - defaulting to XML</li>
5656 <li><a href="<?php echo site_url ('api/example/user/id/1/format/json ' );?> ">User #1</a> - get it in JSON</li>
57+ <li><a id="ajax" href="<?php echo site_url ('api/example/users/format/json ' );?> ">Users</a> - get it in JSON (AJAX request)</li>
5758</ul>
5859
5960<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
6061
61-
6262<p><br />Page rendered in {elapsed_time} seconds</p>
6363
64+ <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
65+ <script type="text/javascript">
66+ $(function(){
67+ // Bind a click event to the 'ajax' object id
68+ $("#ajax").bind("click", function( evt ){
69+ // Javascript needs totake over. So stop the browser from redirecting the page
70+ evt.preventDefault();
71+ // AJAX request to get the data
72+ $.ajax({
73+ // URL from the link that was clicked on
74+ url: $(this).attr("href"),
75+ // Success function. the 'data' parameter is an array of objects that can be looped over
76+ success: function(data, textStatus, jqXHR){
77+ alert('Successful AJAX request!');
78+ },
79+ // Failed to load request. This could be caused by any number of problems like server issues, bad links, etc.
80+ error: function(jqXHR, textStatus, errorThrown){
81+ alert('Oh no! A problem with the AJAX request!');
82+ }
83+ });
84+ });
85+ });
86+ </script>
87+
6488</body>
6589</html>
0 commit comments