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
Makarand Bhatambarekar
 
PPTX
jQuery
Vishwa Mohan
 
PPT
Xml http request
Jayalakshmi Ayyappan
 
PDF
Ajax
gauravashq
 
PDF
Webdevelopment
Giacomo Antonino Fazio
 
PPTX
SharePoint and jQuery Essentials
Mark Rackley
 
PPT
Ajax
Manav Prasad
 
PPTX
e-suap - client technologies- english version
Sabino Labarile
 
JavaScript JQUERY AJAX
Makarand Bhatambarekar
 
jQuery
Vishwa Mohan
 
Xml http request
Jayalakshmi Ayyappan
 
Webdevelopment
Giacomo Antonino Fazio
 
SharePoint and jQuery Essentials
Mark Rackley
 
e-suap - client technologies- english version
Sabino Labarile
 

What's hot (20)

PPTX
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
PDF
jQuery - Chapter 1 - Introduction
WebStackAcademy
 
PDF
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
PDF
jQuery - Chapter 3 - Effects
WebStackAcademy
 
PPT
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
 
KEY
Summer - The HTML5 Library for Java and Scala
rostislav
 
PPT
A Short Introduction To jQuery
Sudar Muthu
 
PPT
Jquery 4
Manish Kumar Singh
 
PPT
jQuery for beginners
Divakar Gu
 
PDF
jQuery -Chapter 2 - Selectors and Events
WebStackAcademy
 
PPTX
Getting Started with jQuery
Akshay Mathur
 
PDF
[2015/2016] Local data storage for web-based mobile apps
Ivano Malavolta
 
PPTX
Jqueryppt (1)
AndreaSmile06
 
PPTX
Client-side Rendering with AngularJS
David Lapsley
 
PDF
Not your Grandma's XQuery
William Candillon
 
PDF
Write Less Do More
Remy Sharp
 
PDF
XQuery Rocks
William Candillon
 
PPTX
20141001 delapsley-oc-openstack-final
David Lapsley
 
PDF
jQuery Essentials
Bedis ElAchèche
 
PPTX
Unobtrusive javascript with jQuery
Angel Ruiz
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
jQuery - Chapter 1 - Introduction
WebStackAcademy
 
jQuery - Chapter 4 - DOM Handling
WebStackAcademy
 
jQuery - Chapter 3 - Effects
WebStackAcademy
 
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
 
Summer - The HTML5 Library for Java and Scala
rostislav
 
A Short Introduction To jQuery
Sudar Muthu
 
jQuery for beginners
Divakar Gu
 
jQuery -Chapter 2 - Selectors and Events
WebStackAcademy
 
Getting Started with jQuery
Akshay Mathur
 
[2015/2016] Local data storage for web-based mobile apps
Ivano Malavolta
 
Jqueryppt (1)
AndreaSmile06
 
Client-side Rendering with AngularJS
David Lapsley
 
Not your Grandma's XQuery
William Candillon
 
Write Less Do More
Remy Sharp
 
XQuery Rocks
William Candillon
 
20141001 delapsley-oc-openstack-final
David Lapsley
 
jQuery Essentials
Bedis ElAchèche
 
Unobtrusive javascript with jQuery
Angel Ruiz
 
Ad

Viewers also liked (9)

PPTX
Introduction to AJAX
jtedesco5
 
PDF
'Less' css
Mayur Mohite
 
PPTX
Preprocessor CSS: SASS
Geoffroy Baylaender
 
PPTX
Ajax xml json
Andrii Siusko
 
PPTX
Ajax and Jquery
People Strategists
 
PDF
jQuery and Ajax
Anton Tibblin
 
PPTX
Introduction to JSON & AJAX
Collaboration Technologies
 
PDF
Introduction to JSON
Kanda Runapongsa Saikaew
 
PDF
jQuery for beginners
Siva Arunachalam
 
Introduction to AJAX
jtedesco5
 
'Less' css
Mayur Mohite
 
Preprocessor CSS: SASS
Geoffroy Baylaender
 
Ajax xml json
Andrii Siusko
 
Ajax and Jquery
People Strategists
 
jQuery and Ajax
Anton Tibblin
 
Introduction to JSON & AJAX
Collaboration Technologies
 
Introduction to JSON
Kanda Runapongsa Saikaew
 
jQuery for beginners
Siva Arunachalam
 
Ad

Similar to Simplify AJAX using jQuery (20)

PDF
Jquery ajax post example
Trà Minh
 
KEY
Alpha Streaming Realtime
Mark Fayngersh
 
PPTX
Ajax for dummies, and not only.
Nerd Tzanetopoulos
 
PDF
Os Pruett
oscon2007
 
PDF
Understanding backbonejs
Nick Lee
 
KEY
Ajax - a quick introduction
Stefan Pettersson
 
PDF
Specification-Driven Development of REST APIs by Alexander Zinchuk
OdessaJS Conf
 
PDF
Web 11 | AJAX + JSON + PHP
Mohammad Imam Hossain
 
PDF
jQuery secrets
Bastian Feder
 
PDF
How to make Ajax work for you
Simon Willison
 
PPT
AJAX.ppt
karan419841
 
PDF
jQuery's Secrets
Bastian Feder
 
PDF
jQuery and Rails, Sitting in a Tree
adamlogic
 
PDF
async/await in Swift
Peter Friese
 
PPTX
Bare-knuckle web development
Johannes Brodwall
 
PDF
Jersey
Yung-Lin Ho
 
PDF
Drupal Mobile
Ruben Teijeiro
 
PDF
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Bastian Hofmann
 
PPTX
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Ayes Chinmay
 
PDF
Mashing up JavaScript
Bastian Hofmann
 
Jquery ajax post example
Trà Minh
 
Alpha Streaming Realtime
Mark Fayngersh
 
Ajax for dummies, and not only.
Nerd Tzanetopoulos
 
Os Pruett
oscon2007
 
Understanding backbonejs
Nick Lee
 
Ajax - a quick introduction
Stefan Pettersson
 
Specification-Driven Development of REST APIs by Alexander Zinchuk
OdessaJS Conf
 
Web 11 | AJAX + JSON + PHP
Mohammad Imam Hossain
 
jQuery secrets
Bastian Feder
 
How to make Ajax work for you
Simon Willison
 
AJAX.ppt
karan419841
 
jQuery's Secrets
Bastian Feder
 
jQuery and Rails, Sitting in a Tree
adamlogic
 
async/await in Swift
Peter Friese
 
Bare-knuckle web development
Johannes Brodwall
 
Jersey
Yung-Lin Ho
 
Drupal Mobile
Ruben Teijeiro
 
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Bastian Hofmann
 
Internet and Web Technology (CLASS-8) [jQuery and JSON] | NIC/NIELIT Web Tech...
Ayes Chinmay
 
Mashing up JavaScript
Bastian Hofmann
 

More from Siva Arunachalam (17)

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

Recently uploaded (20)

PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Software Development Methodologies in 2025
KodekX
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Software Development Methodologies in 2025
KodekX
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Doc9.....................................
SofiaCollazos
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Architecture of the Future (09152021)
EdwardMeyman
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 

Simplify AJAX using jQuery