SlideShare a Scribd company logo
Simplify AJAX using
      jQuery
                                        December 24, 2012



     Sivasubramaniam Arunachalam
                       @sivaa_in
      http://www.meetup.com/Online-Technology-User-Group/events/87541132/
It’s me!

• Application Developer
• Technical Consultant
• Process Mentor
It’s about you!


Web(Application) Developer &
      Tasted jQuery
Expectations
•   Introduction
•   No Server Specific
•   Not a tutorial
•   Not a reference guide
Introduction to AJAX
Asynchronous
     - No refresh
     - No redirection
     - Same Lifeline
JavaScript
And XML
XMLHttpRequest
Google and AJAX
AJAX – The Flow




http://www.cs.uky.edu/~paulp/CS316F12/CS316AJAX_html_m79697898.png
http://www.cs.uky.edu/~paulp/CS316F12/CS316AJAX_html_m79697898.png
http://www.cs.uky.edu/~paulp/CS316F12/CS316AJAX_html_m79697898.png
AJAX in Javascript - Initialize
AJAX in Javascript - Callback
AJAX in Javascript - Send
Simplify AJAX using jQuery
Simplify AJAX using jQuery
HTTP Cache related Fields

• Expires
• Last-Modified
• Cache-Control
Expires
Last-Modified
Cache-Control
POST - n/a
Caching and AJAX
• Caching is always good
• Same as HTTP Caching
  • Static Content
     •   Lookup Data
  • Images
• Not good for AJAX
  • Mostly used with Dynamic Content
IE always Caches AJAX requests
         by Default
Simplify AJAX using jQuery
Simple Hack

Just add random number to
        request URL
The jQuery Way
Simple AJAX request



  load()
Simple AJAX request

Syntax:

$(“selector”).load(URL);
Simple AJAX request

Example:

$(“#myDiv”).load(“/get.html”);
Event are Important

Example:
$(“#myButton”).click(function(event){
    $(“#myDiv”).load(“/get.html”);
});
Process a Part
url = ajax/load_basic
url = ajax/load_basic #image
      <dom id=“image”>…</dom>

Example:
<a href=“url" id="image">
      <img src=“img.jpg”>
</a>
Better AJAX request
Syntax:
$(“selector”).load(
        URL,
        [data],
        [callback]
);
Better AJAX request - Methods
    Syntax:
    $(“selector”).load(
            URL,
            [data],    GET / POST

            [callback]
    );
Better AJAX request - GET
   Syntax:
   $(“selector”).load(
            URL,
            [data],    GET
            [callback]
   );
        ”company=yahoo&loc=india”
Better AJAX request - GET

$(“selector”).load(
    “ajax/getdata”,
     ”company=yahoo&loc=india”
);
Better AJAX request - POST
   Syntax:
   $(“selector”).load(
             URL,
             [data],
                               POST
             [callback]
   );
        { company: ‘yahoo’,
                loc: ‘india’ }
Better AJAX request - POST

$(“selector”).load(
    “ajax/getdata”,
     { company: ‘yahoo’, loc: ‘india’ }
);
Better AJAX request
Syntax:
$(“selector”).load(
          URL,
          [data],
          [callback]
); (responseText, textStatus, XMLHttpRequest)
AJAX, jQuery &
   JSON
JSON
JavaScript Object Notation
• data-interchange format
  •   Light weight
  •   Human readable
  •   And better than XML
  •   Now language independent
JSON - Example
var json =
 {
    “id”: “23IT64”,
    “name”: {
                      “first”: “Sivasubramaniam”,
                      “last”: “Arunachalam”
              },
    “profession”: “developer”
}
JSON – Access (1)
var json =
{

    “id”: “23IT64”,
    “name”: {
                  “first”: “Sivasubramaniam”,
                  “last”: “Arunachalam”
              },
    “profession”: “developer”         json.id
}
JSON – Access (2)
var json =
{                         json.name.first
    ”id”: “23IT64”,
    “name”: {
                      “first”: “Sivasubramaniam”,
                      “last”: “Arunachalam”
              },
    “profession”: “developer”
}
AJAX - JSON Request
Syntax:
$.getJSON(
        URL,
        [data],
        [success callback]
);
AJAX - JSON Request
                         ”company=yahoo&loc=india”
Syntax:
                                 GET
$.getJSON(        { company: ‘yahoo’, loc: ‘india’ }

        URL,       No Post & GET               only

        [data],
        [success callback]
);
AJAX - JSON Request
Syntax:             Success_callback(
                         json_data,
$.getJSON(               [textStatus],
        URL,             [jqXHR]
                    )
        [data],
        [success callback]
);
AJAX - JSON Request - Example
$.getJSON (
     “ajax/getjson”,
     ”company=yahoo&loc=india”,

     function(json) {
          $("#mydiv01").html(json.id);
          $("#mydiv02").html(json.profession);
     }
);
AJAX, jQuery &
Remote Script
Let’s load a piece of
Javascript from Server
and execute it.
AJAX – Script Request
Syntax:

$.getScript(
       URL,
       [success callback]
);
AJAX – Script Request
Syntax:           success_callback(
                       [script_data],
$.getScript(           [textStatus],
                       [jqXHR]
       URL,       )
       [success callback]
);
AJAX – Script Request - Example

$.getScript(
     “ajax/getscript”,
     function() {
            $("#mydiv").html(“Script Loaded..”);
     }
);       Script will be executed automatically!
A Simple GET   AJAX Request
AJAX – GET Request
Syntax:
$.get(
          URL,
          [data],
          [success callback],
          [response data type]
);
AJAX – GET Request
Syntax:
                  { company: ‘yahoo’, loc: ‘india’ }
$.get(
          URL,
          [data],
          [success callback],
          [response data type]
);
AJAX – GET Request
Syntax:               success_callback(
$.get(                     data,
                           [textStatus],
          URL,             [jqXHR]
          [data],     )
          [success callback],
          [response data type]
);
AJAX – GET Request
Syntax:                      •   html
$.get(                       •   xml
                             •   json
          URL,               •   script
          [data],
          [success callback],
          [response data type]
);
AJAX – GET Request - Example
$.get(
     “ajax/getdata”,
     { company: ‘yahoo’, loc: ‘india’ },
     function(responseText) {
          $("#content").html(responseText)
     },
     “html”
);
A Simple POST   AJAX Request
AJAX – POST Request
Syntax:
$.post(
          URL,
          [data],
          [success callback],
          [response data type]
);
AJAX – POST Request
Syntax:
                  { company: ‘yahoo’, loc: ‘india’ }
$.post(
          URL,
          [data],
          [success callback],
          [response data type]
);
AJAX – POST Request
Syntax:               success_callback(
$.post(                    data,
                           [textStatus],
          URL,             [jqXHR]
          [data],     )
          [success callback],
          [response data type]
);
AJAX – POST Request
Syntax:                      •   html
$.post(                      •   xml
                             •   json
          URL,               •   script
          [data],
          [success callback],
          [response data type]
);
AJAX – POST Request - Example
$.post(
     “ajax/postdata”,
     { company: ‘yahoo’, loc: ‘india’ },
     function(responseText) {
          $("#content").html(responseText)
     },
     “html”
);
And we are done with the
      “basics”
And what happens when AJAX
       requests are
      “failed”?
Lets do advanced AJAX 
Global Setup
                $.ajaxSetup()
                 •   async
•   type                           •   global
                 •   cache         •
•   url                                beforeSend()
•   username     •   timeout       •   complete()
•   password     •   contentType   •   success()
                 •   dataType      •   error()
               Override is possible
Global AJAX Event Handlers
• .ajaxSend() • .ajaxComplete()
• .ajaxStart() • .ajaxSuccess ()
• .ajaxStop() • .ajaxError()
         • .done()
         • .fail()
         • .always()
.ajaxStart() & .ajaxStop()
var ajax_load = “<img src=“loading.gif”/>

$("*").ajaxStart(function(){
     $("#loading").html(ajax_load);
});

$("*").ajaxStop(function(){
     $("#loading").html("");
});
.ajax() - Examples
$.ajax ( {
     url: “ajax/load“,
     success: function(data) {
           $(‘#result').html(data);
     }
});
.ajax() - Examples
$.ajax ( {
     url: “ajax/get“,
     type: “GET“,
     cache: true
}).done(function( data) {
      $("#results").html(data);
});
.ajax() - Examples
$.ajax ( {
     url: “ajax/post“,
     type: “POST“,
     data: { company: “yahoo", loc: “india" }
}).done(function( data) {
      $("#results").html(data);
});
.ajax() - Examples

$.ajax ( {
     url: “ajax/js“,
     type: “GET“,
     dataType: “script”,
});
.ajax() - Examples

$.ajax ( “ajax/load" )
     .done (function() { alert("success"); })
     .fail (function() { alert("error"); })
     .always (function() { alert("complete") });
Summary




http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
Demo
Thank You!
            siva@sivaa.in
https://www.facebook.com/sivasubramaniam.arun
References
•   http://www.clickonf5.org/2902/schedule-meeting-send-invitation-gmail/
•   http://explainafide.com.au/rss-feed-reader/
•   http://squash2020.com/squash-tops-google-search/google-map-logo/
•   http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
•   http://stevesouders.com/hpws/rule-ajax.php
•   http://blog.httpwatch.com/2009/08/07/ajax-caching-two-important-facts/
•   http://viralpatel.net/blogs/ajax-cache-problem-in-ie/
•   http://www.tutorialspoint.com/jquery/jquery-ajax.htm

More Related Content

PPT
JavaScript JQUERY AJAX
PPTX
jQuery
PPT
Xml http request
PDF
PDF
Webdevelopment
PPTX
SharePoint and jQuery Essentials
PPT
PPTX
e-suap - client technologies- english version
JavaScript JQUERY AJAX
jQuery
Xml http request
Webdevelopment
SharePoint and jQuery Essentials
e-suap - client technologies- english version

What's hot (20)

PPTX
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
PDF
jQuery - Chapter 1 - Introduction
PDF
jQuery - Chapter 4 - DOM Handling
PDF
jQuery - Chapter 3 - Effects
PPT
Asynchronous JavaScript & XML (AJAX)
KEY
Summer - The HTML5 Library for Java and Scala
PPT
A Short Introduction To jQuery
PPT
PPT
jQuery for beginners
PDF
jQuery -Chapter 2 - Selectors and Events
PPTX
Getting Started with jQuery
PDF
[2015/2016] Local data storage for web-based mobile apps
PPTX
Jqueryppt (1)
PPTX
Client-side Rendering with AngularJS
PDF
Not your Grandma's XQuery
PDF
Write Less Do More
PDF
XQuery Rocks
PPTX
20141001 delapsley-oc-openstack-final
PDF
jQuery Essentials
PPTX
Unobtrusive javascript with jQuery
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
jQuery - Chapter 1 - Introduction
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 3 - Effects
Asynchronous JavaScript & XML (AJAX)
Summer - The HTML5 Library for Java and Scala
A Short Introduction To jQuery
jQuery for beginners
jQuery -Chapter 2 - Selectors and Events
Getting Started with jQuery
[2015/2016] Local data storage for web-based mobile apps
Jqueryppt (1)
Client-side Rendering with AngularJS
Not your Grandma's XQuery
Write Less Do More
XQuery Rocks
20141001 delapsley-oc-openstack-final
jQuery Essentials
Unobtrusive javascript with jQuery
Ad

Viewers also liked (9)

PPTX
Introduction to AJAX
PDF
'Less' css
PPTX
Preprocessor CSS: SASS
PPTX
Ajax xml json
PPTX
Ajax and Jquery
PDF
jQuery and Ajax
PPTX
Introduction to JSON & AJAX
PDF
Introduction to JSON
PDF
jQuery for beginners
Introduction to AJAX
'Less' css
Preprocessor CSS: SASS
Ajax xml json
Ajax and Jquery
jQuery and Ajax
Introduction to JSON & AJAX
Introduction to JSON
jQuery for beginners
Ad

Similar to Simplify AJAX using jQuery (20)

PPTX
Ajax for dummies, and not only.
PPT
AJAX.ppt
PPTX
PPTX
JSON and XML
PPT
jQuery Ajax
PDF
1 ppt-ajax with-j_query
PPT
PPT
Ajax and PHP
PDF
Ajax
PPTX
Web technologies-course 10.pptx
PPT
Ajax Lecture Notes
PPT
Mashup
PPTX
AJAX.pptx
PDF
How to Use AJAX in PHP and jQuery.pdf
PDF
Ajax
PPT
Ajax introduction
PPTX
Web-Engineering-Lec-14 (1 ).pptx
PPTX
Web-Engineering-Lec-14 (1) .pptx
Ajax for dummies, and not only.
AJAX.ppt
JSON and XML
jQuery Ajax
1 ppt-ajax with-j_query
Ajax and PHP
Ajax
Web technologies-course 10.pptx
Ajax Lecture Notes
Mashup
AJAX.pptx
How to Use AJAX in PHP and jQuery.pdf
Ajax
Ajax introduction
Web-Engineering-Lec-14 (1 ).pptx
Web-Engineering-Lec-14 (1) .pptx

More from Siva Arunachalam (17)

PDF
Introduction to EDI(Electronic Data Interchange)
PDF
Introduction to logging in django
PDF
Introduction to Test Driven Development
PDF
Setup a New Virtualenv for Django in Windows
PDF
What's New in Django 1.6
PDF
Introduction to Browser Internals
PDF
Web sockets in java EE 7 - JavaOne 2013
PDF
Python for High School Programmers
PDF
Introduction to Cloud Computing
PDF
Web Sockets in Java EE 7
PDF
Introduction to Browser DOM
PDF
Installing MySQL for Python
PDF
Using Eclipse and Installing PyDev
PPT
Installing Python 2.7 in Windows
PDF
Setup a New Virtualenv for Django in Windows
PPT
Introduction to Google APIs
PDF
Introduction to Django
Introduction to EDI(Electronic Data Interchange)
Introduction to logging in django
Introduction to Test Driven Development
Setup a New Virtualenv for Django in Windows
What's New in Django 1.6
Introduction to Browser Internals
Web sockets in java EE 7 - JavaOne 2013
Python for High School Programmers
Introduction to Cloud Computing
Web Sockets in Java EE 7
Introduction to Browser DOM
Installing MySQL for Python
Using Eclipse and Installing PyDev
Installing Python 2.7 in Windows
Setup a New Virtualenv for Django in Windows
Introduction to Google APIs
Introduction to Django

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Modernizing your data center with Dell and AMD
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
NewMind AI Monthly Chronicles - July 2025
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
Modernizing your data center with Dell and AMD
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Digital-Transformation-Roadmap-for-Companies.pptx

Simplify AJAX using jQuery