Skip to content

Commit 37e47c7

Browse files
author
Phil Sturgeon
committed
2 parents 3a39061 + 3468f54 commit 37e47c7

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

application/config/rest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,20 @@
242242
*/
243243
$config['rest_ignore_http_accept'] = FALSE;
244244

245+
/*
246+
|--------------------------------------------------------------------------
247+
| REST AJAX Only
248+
|--------------------------------------------------------------------------
249+
|
250+
| Set to TRUE to only allow AJAX requests. If TRUE and the request is not
251+
| coming from AJAX, a 505 response with the error message "Only AJAX
252+
| requests are accepted." will be returned. This is good for production
253+
| environments. Set to FALSE to also accept HTTP requests.
254+
|
255+
| FALSE
256+
|
257+
*/
258+
$config['rest_ajax_only'] = FALSE;
259+
245260
/* End of file config.php */
246261
/* Location: ./system/application/config/rest.php */

application/libraries/REST_Controller.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232

3333
// Lets grab the config and get ready to party
3434
$this->load->config('rest');
35-
35+
3636
// How is this request being made? POST, DELETE, GET, PUT?
3737
$this->request->method = $this->_detect_method();
3838

@@ -111,6 +111,12 @@ public function __construct()
111111
{
112112
$this->_allow = $this->_detect_api_key();
113113
}
114+
115+
// only allow ajax requests
116+
if( ! $this->input->is_ajax_request() AND config_item('rest_ajax_only') )
117+
{
118+
$this->response( array('error' => 'Only AJAX requests are accepted.'), 505 );
119+
}
114120
}
115121

116122
/*

application/views/welcome_message.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,36 @@
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

Comments
 (0)