SlideShare a Scribd company logo
101 
Hernán Garzón de la Roza 
@chertopjanov
Agenda: 101 
- AJAX basic concepts 
- HTTP methods GET / POST / PUT / OTHERS 
- TEXT / XML / JSON 
- From the server to Javascript: Parsing the response 
- jQuery and AJAX
Ajax basics
A 
J 
A 
X 
101
A 
J 
A 
X 
synchronous 
101
101 
Synchronous call
101 
Browser 
Time 
Server
101 
Browser 
Server 
User action User action User action 
Time 
Server 
processing 
Server 
processing
101 
Asynchronous call
101 
user action 
Browser 
Ajax engine 
Server 
Server processing
101 
Browser 
Server 
User action 
Response 
Time 
Server 
processing 
Ajax engine
101 
Browser 
Server 
User action 
Response 
Time 
Server 
processing 
Ajax engine
A 
J 
A 
X 
synchronous 
avascript 
101
101
XMLHttpRequest (XHR) Object 
101
101 
- Attributes 
- readyState 
- responseText 
- status 
- statusText 
- Events 
- Methods 
- onreadystatechange 
- onload 
- onloadstart 
- on progress 
- open 
- send 
- abort
Compatibility and support 
101
A 
J 
A 
X 
synchronous 
avascript 
and 
101
A 
J 
A 
X 
synchronous 
avascript 
and 
ml 
101
101 
How AJAX works step by step
101 
Create an XMLHttpRequest object 
var req = new XMLHttpRequest();
101 
Create a callback function 
xhr.onreadystatechange = function () { 
// runs every time there is a change in the event. 
}
101 
xhr.onreadystatechange 
0 1 2 3 4 >> readyState
101 
Create a callback function 
xhr.onreadystatechange = function () { 
if (xhr.readyState === 4){...} 
}
101 
Open a request 
xhr.open(‘GET’, ‘sidebar.html’);
101 
open(httpMethod, url, boolean)
101 
HTTP GET method 
Receiving or getting information from a server 
https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods
101 
HTTP GET method 
Static and dynamic information 
https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods
101 
Dynamic QueryString 
http://www.website.com/employee.php?lastName=perez 
http://www.url-encode-decode.com/
101 
HTTP POST method 
Sending information that’s gonna be saved 
https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods
101 
HTTP PUT method 
Puts a file or resource at a specific URI, and exactly at that URI. 
https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods
101 
Send the request 
xhr.send();
xhr.onreadystatechange = function () { 101 
if (xhr.readyState === 4){ 
// Where the magic happens 
} 
} 
xhr.open(‘GET’, ‘data/employees.json’); 
xhr.send();
101 
demo basic ajax
101 
Response 
Reply to AJAX requests
101 
request 
Browser Server 
response
101 
Simple text response 
plain text or HTML
101 
Request a static file
101 
Lots of data 
and how to handle it
101 
Structured data format 
XML & JSON
101 
XML 
Tags to structure data
101 
<xml> 
<employees> 
<employee> 
<firstName>John</firstName> 
<lastName>Doe</lastName> 
</employee> 
<employee> 
<firstName>Anna</firstName> 
<lastName>Smith</lastName> 
</employee> 
<employee> 
<firstName>Peter</firstName> 
<lastName>Jones</lastName> 
</employee> 
</employees> 
</xml>
101 
JSON 
JavaScript Object Notation
101 
JSON 
Array notation
101
101 
[‘string’,2, true, [1,2,3,4]]
101 
JSON 
Object notation
101 
{"employees":[ 
{"firstName":"John", "lastName":"Doe"}, 
{"firstName":"Anna", "lastName":"Smith"}, 
{"firstName":"Peter", "lastName":"Jones"} 
]} 
http://jsonlint.com/
101 
Parsing 
Convert plain text into javascript
101 
demo parsing
101 
Limits
101 
Web Browser same origin policy 
http://en.wikipedia.org/wiki/Same_origin_policy
101
101 
http://www.myserver.com http://www.myserver.com/ajax.html 
Browser Server
101
101 
http://www.myserver.com http://www.anotherwebsite.com 
Browser Server
101 
http://www.myserver.com https://www.myserver.com/8888 
Browser Server
101 
http://www.myserver.com https://www.db.myserver.com/ 
Browser Server
101 
JSONP 
JSON with padding
101 
https://www.myserver.com/ 
Server 
http://www.myserver.com/home 
Browser
101 
http://www.myserver.com/home 
https://www.anotherserver.com/ 
Browser 
Server 
https://www.myserver.com/ 
Server
101 
http://www.myserver.com/home 
https://www.anotherserver.com/ 
Browser 
Server 
https://www.myserver.com/ 
Server 
<script src=”http://anotherserver.com/jsFile.js”></script>
101 
Cross-Origin Resource Sharing 
Access-Control-Allow-Origin
101 
jQuery 
http://api.jquery.com/category/ajax/shorthand-methods/
101 
Load the jQuery library CDN 
<script src=”http://code.jquery.com/jquery-1.11.9.min.js”></script>
101 
$.get();
101 
$.post();
101 
$.getJson();
101 
demo $.load();
101 
$.ajax(url, settings); 
http://librojquery.com/#opciones-del-método-.ajax 
http://api.jquery.com/jquery.ajax/
101 
$.ajax(url, settings); 
var url = $(this).attr(‘action’);
101 
$.ajax(url, settings); 
$.ajax(url, { 
data: formData, 
type: ‘POST’, 
success: function(response){ 
$(‘signup’).html(‘<p>Thank you!</p>’) 
} 
});
101 
var url = $(this).attr(‘action’); 
$.ajax(url, { 
data: formData, 
type: ‘POST’, 
success: function(response){ 
$(‘signup’).html(‘<p>Thank you!</p>’) 
} 
});
101 
$.post vs $.ajax
101 
<h1>Sign up for our newsletter:</h1> 
<div id=”signup”> 
<form method=”post” action=”/signup”> 
<label for=”userName”>Nombre:</label> 
<input type=”text” name=”userName” id=”userName”> 
<label for=”email”>Email:</label> 
<input type=”email” name=”email” id=”email”> 
<label for=”submit”></label> 
<input type=”submit” value=”signUp!” id=”sumbit”> 
</form> 
</div>
101 
$(‘form’).submit(function(evt) { 
evt.preventDefault(); 
var url = $(this).attr(“action”); 
var formData = $(this).serialize(); 
$.post(url,formData,function(response){ 
$(‘#signup’).html(‘<p>thanks for signing 
up!</p>’); 
}); // end post 
}); // end submit
101 
$(‘form’).submit(function(evt) { 
evt.preventDefault(); 
var url = $(this).attr(“action”); 
var formData = $(this).serialize(); 
$.ajax({ 
url: url, 
data: formData, 
type: “POST”, 
success: function(response){ 
$(‘#signup’).html(‘<p>thanks for signing 
up!</p>’) 
}); 
)}; // end post 
}); // end sumbit
Ajax basics
Thank you

More Related Content

PDF
Vladimir Vorontsov - Splitting, smuggling and cache poisoning come back
PPTX
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
PDF
OWASP Proxy
PDF
Varnish Cache and Django (Falcon, Flask etc)
ODP
Beyond php - it's not (just) about the code
PDF
Top Node.js Metrics to Watch
PDF
Codified PostgreSQL Schema
PDF
Facebook的缓存系统
Vladimir Vorontsov - Splitting, smuggling and cache poisoning come back
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
OWASP Proxy
Varnish Cache and Django (Falcon, Flask etc)
Beyond php - it's not (just) about the code
Top Node.js Metrics to Watch
Codified PostgreSQL Schema
Facebook的缓存系统

What's hot (20)

PDF
Py conkr 20150829_docker-python
PDF
톰캣 #04-환경설정
ODP
Remove php calls and scale your site like crazy !
PDF
Http capturing
PDF
Roll Your Own API Management Platform with nginx and Lua
PDF
Debugging: Rules & Tools
PDF
Shell Script Disk Usage Report and E-Mail Current Threshold Status
PDF
4069180 Caching Performance Lessons From Facebook
PDF
Pycon - Python for ethical hackers
ODP
The why and how of moving to PHP 5.4/5.5
ODP
The why and how of moving to PHP 5.5/5.6
ODP
When dynamic becomes static: the next step in web caching techniques
PDF
WSO2Con USA 2015: Securing your APIs: Patterns and More
PDF
Perlmania_Study - CPAN
PPT
On UnQLite
PDF
Lua tech talk
PDF
わかった気になるgitit-0.8
PPTX
Administering and Monitoring SolrCloud Clusters
PPTX
Advanced HTTP Caching
PPT
php $_GET / $_POST / $_SESSION
Py conkr 20150829_docker-python
톰캣 #04-환경설정
Remove php calls and scale your site like crazy !
Http capturing
Roll Your Own API Management Platform with nginx and Lua
Debugging: Rules & Tools
Shell Script Disk Usage Report and E-Mail Current Threshold Status
4069180 Caching Performance Lessons From Facebook
Pycon - Python for ethical hackers
The why and how of moving to PHP 5.4/5.5
The why and how of moving to PHP 5.5/5.6
When dynamic becomes static: the next step in web caching techniques
WSO2Con USA 2015: Securing your APIs: Patterns and More
Perlmania_Study - CPAN
On UnQLite
Lua tech talk
わかった気になるgitit-0.8
Administering and Monitoring SolrCloud Clusters
Advanced HTTP Caching
php $_GET / $_POST / $_SESSION
Ad

Viewers also liked (11)

PDF
Mike Davies - Ajax And Accessibility
PPTX
PDF
Web 2.0 & Ajax Basics
PPT
Html For Beginners 2
PPT
Ajax basic intro
PDF
Ajax basics
PPTX
Introduction to AJAX
PPTX
What is Ajax technology?
PPT
Html for Beginners
PPTX
Presentation html
PDF
HTML CSS Basics
Mike Davies - Ajax And Accessibility
Web 2.0 & Ajax Basics
Html For Beginners 2
Ajax basic intro
Ajax basics
Introduction to AJAX
What is Ajax technology?
Html for Beginners
Presentation html
HTML CSS Basics
Ad

Similar to Ajax basics (20)

PDF
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
PPTX
Rapid API development examples for Impress Application Server / Node.js (jsfw...
PDF
GraphConnect 2014 SF: From Zero to Graph in 120: Scale
PDF
Caching the Uncacheable
PDF
Android dev 3
PDF
Taking Laravel to the edge with HTTP caching and Varnish
KEY
DVWA BruCON Workshop
PDF
Going crazy with Varnish and Symfony
PPT
Jsp/Servlet
PDF
Java clients for elasticsearch
PPTX
Cache is King
ODP
Implementing Comet using PHP
PPTX
Unit-5.pptx
PDF
Cross Origin Communication (CORS)
KEY
Message in a Bottle
KEY
Site Performance - From Pinto to Ferrari
PPTX
Whats new in ASP.NET 4.0
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
PPTX
Web Real-time Communications
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
Rapid API development examples for Impress Application Server / Node.js (jsfw...
GraphConnect 2014 SF: From Zero to Graph in 120: Scale
Caching the Uncacheable
Android dev 3
Taking Laravel to the edge with HTTP caching and Varnish
DVWA BruCON Workshop
Going crazy with Varnish and Symfony
Jsp/Servlet
Java clients for elasticsearch
Cache is King
Implementing Comet using PHP
Unit-5.pptx
Cross Origin Communication (CORS)
Message in a Bottle
Site Performance - From Pinto to Ferrari
Whats new in ASP.NET 4.0
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Real-time Communications

Recently uploaded (20)

PPTX
areprosthodontics and orthodonticsa text.pptx
PPTX
Implications Existing phase plan and its feasibility.pptx
PDF
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
PDF
Quality Control Management for RMG, Level- 4, Certificate
DOCX
The story of the first moon landing.docx
PDF
YOW2022-BNE-MinimalViableArchitecture.pdf
PDF
High-frequency high-voltage transformer outline drawing
PPTX
YV PROFILE PROJECTS PROFILE PRES. DESIGN
PDF
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
DOCX
actividad 20% informatica microsoft project
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PDF
Interior Structure and Construction A1 NGYANQI
PPTX
mahatma gandhi bus terminal in india Case Study.pptx
PDF
The Advantages of Working With a Design-Build Studio
PDF
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
PPTX
HPE Aruba-master-icon-library_052722.pptx
PDF
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
PPT
UNIT I- Yarn, types, explanation, process
areprosthodontics and orthodonticsa text.pptx
Implications Existing phase plan and its feasibility.pptx
Emailing DDDX-MBCaEiB.pdf DDD_Europe_2022_Intro_to_Context_Mapping_pdf-165590...
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
Quality Control Management for RMG, Level- 4, Certificate
The story of the first moon landing.docx
YOW2022-BNE-MinimalViableArchitecture.pdf
High-frequency high-voltage transformer outline drawing
YV PROFILE PROJECTS PROFILE PRES. DESIGN
Design Thinking - Module 1 - Introduction To Design Thinking - Dr. Rohan Dasg...
actividad 20% informatica microsoft project
AD Bungalow Case studies Sem 2.pptxvwewev
Interior Structure and Construction A1 NGYANQI
mahatma gandhi bus terminal in india Case Study.pptx
The Advantages of Working With a Design-Build Studio
Facade & Landscape Lighting Techniques and Trends.pptx.pdf
HPE Aruba-master-icon-library_052722.pptx
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
UNIT I- Yarn, types, explanation, process

Ajax basics

Editor's Notes

  • #38: http://www.w3schools.com/ajax/tryajax_callback.htm
  • #50: A pesar de que la respuesta del server parece JavaScript, NO ES JAVASCRIPT. Es solamente string en texto plano. Para usar JSON, tenemos que tomar este string y convertirlo en JS. Este proceso se llama PARSING.