1. Place ajax.php in your application/libraries folder.
2. Add the following code to your application/config/constants.php file:
/*
|--------------------------------------------------------------------------
| Detect Ajax Requests
|--------------------------------------------------------------------------
|
| This constant is used to determine whether the request is an AJAX request, or a standard HTTP request.
|
*/
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
3. Place encode_xml_helper.php in your application/helpers folder (or use your own xml encoder) if you plan on sending back xml.
4. Place test.php in your application/controllers folder and ajax_test_view.php in your application/views/test folder.
Example controller (test.php)
class Test extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('test/ajax_test_view');
}
function get_something()
{
$this->load->library('ajax');
$arr['something'] = 'Something Good';
if ($this->input->post('something_id') == '2')
{
$arr['something'] = 'Something Better';
}
$this->ajax->output_ajax($arr);
}
}
The example view: ajax_test_view.php
You can limit access to HTTP_X_REQUESTED_WITH requests (ajax requests) two ways. The first is by setting the third parameter of output_ajax to TRUE. Note: this the default. If you try to access your ajax url site_url('/test/get_something') via a web browser you will receive the error message: 'Invalid Request Origin'. If you set it to FALSE you will be able to see the data you are returing in your web browser if you are returing text or html, the json and xml types will prompt a file download.
function get_something()
{
$this->load->library('ajax');
$arr['something'] = 'Something Good';
if ($this->input->post('something_id') == '2')
{
$arr['something'] = 'Something Better';
}
$this->ajax->output_ajax($arr, 'json', TRUE); // Only send a response if this is an ajax request
}
The above method still allows the code in your controler to be executed which is fine for retreving data. If you would like to exit the controler method before any code is executied when this is not an ajax request you can use the non_ajax() method to test it.
function get_something()
{
$this->load->library('ajax');
if ($this->ajax->non_ajax(FALSE)) return; // Exit the controler if this is not an ajax request
// Optionaly passing TRUE will output a 403 HTTP status code with a text/plain message reading "Invalid Request Origin"
// if ($this->ajax->non_ajax(TRUE)) return; // Exit the controler if this is not an ajax request
$arr['something'] = 'Something Good';
if ($this->input->post('something_id') == '2')
{
$arr['something'] = 'Something Better';
}
$this->ajax->output_ajax($arr);
}
Tested with CodeIgniter 2.2.0
codeigniter-jquery-ajax's People
Recommend Projects
-
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
-
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
-
Jobs
Jooble