Professional Documents
Culture Documents
jQuery AJAX
What is AJAX?
AJAX stands for Asynchronous Javascript. It was first mentioned by Jesse James Garret in 2005.
AJAX engine is an XMLHttpRequest object and jQuery makes it a lot more easier for us because
of its AJAX development API. It allows asynchonous communication so instead of freezing up until the
completeness, the browser can communicate with server and continue as normal.
When we use AJAX, there is no noticeable difference because AJAX request does no appear in
the address bar. We can pass and retrive data without refreshing the page or go to a specific URL.
In this chapter, we’re going to learn how to make AJAX request in CodeIgniter
In this controller we have the index method and the content method, both having different
views.
The view contains the button btnloadcontent and a div container content-container. When the
button is clicked, it will load the content from another page (content method of Ajax_tutorial controller)
Output:
Output:
To load the content on the content-container asynchronously on the index method, we’re gonna
use jQuery AJAX. Include the jQuery library and the AJAX request script. Add the following code above
Output:
type (optional)
-this can be post or get method
POST vs GET call in AJAX
-GET puts the parameters in the query string, but POST does not
-GET variables in AJAX requests has still the size limitation on the amount of the data that can be
passed.
GET method is used to retrieved data to display in the page and the data is not expected
to be manipulated with the server while POST method is used to store and/or
manipulate the information on the server
url
-the url to be AJAX requested. it can be a static string or a dynamic string returned by
CodeIgniter’s base_url() method
dataType (optional)
-the most used are html and json. It defines what is the data type of the data retrieved
data (optional)
-this contains the parameters or the data that will be passed
In the above figure, we have used serialize() function that will get all inputs on the form
selected.
We can also use the example below just like get method URLs or query string format:
“txtfname=” + var_fname + “&txtlname=” + var_lname
beforeSend (optional)
-this will be triggered before performing the AJAX communication/request
success
-this will be triggered upon the AJAX request and usually accepts the parameter response
response contains the loaded content from the URL requested.
It can be a string or an object (json object)
error
-this will be triggered if the AJAX request did not meet the URL requested or had no response