79
101 Hernán Garzón de la Roza @chertopjanov

Ajax basics

Embed Size (px)

DESCRIPTION

This presentation will help you understand basics of AJAX.

Citation preview

Page 1: Ajax basics

101

Hernán Garzón de la Roza

@chertopjanov

Page 2: Ajax basics

- AJAX basic concepts

- HTTP methods GET / POST / PUT / OTHERS

- TEXT / XML / JSON

- From the server to Javascript: Parsing the response

- jQuery and AJAX

101Agenda:

Page 3: Ajax basics
Page 4: Ajax basics

AJ

A

X

101

Page 5: Ajax basics

AJ

A

X

synchronous101

Page 6: Ajax basics

101

Synchronous call

Page 7: Ajax basics

101

Browser

Server

Time

Page 8: Ajax basics

101

Browser

Server

User action User action User action

Time

Server processing

Server processing

Page 9: Ajax basics

101

Asynchronous call

Page 10: Ajax basics

101

Server

Browser

user action

Ajax engine

Server processing

Page 11: Ajax basics

101

Browser

Server

User action

Response

Time

Server processing

Ajax engine

Page 12: Ajax basics

101

Browser

Server

User action

Response

Time

Server processing

Ajax engine

Page 13: Ajax basics

AJ

A

X

synchronous

avascript

101

Page 14: Ajax basics

101

Page 15: Ajax basics

XMLHttpRequest (XHR) Object

101

Page 16: Ajax basics

101

- Attributes

- readyState- responseText- status- statusText

- Events

- onreadystatechange- onload- onloadstart- on progress

- Methods

- open- send- abort

Page 17: Ajax basics

Compatibility and support

101

Page 18: Ajax basics

AJ

A

X

synchronous

avascript

and

101

Page 19: Ajax basics

AJ

A

X

synchronous

avascript

and

ml

101

Page 20: Ajax basics

101

How AJAX works step by step

Page 21: Ajax basics

101

Create an XMLHttpRequest object

var req = new XMLHttpRequest();

Page 22: Ajax basics

101

Create a callback function

xhr.onreadystatechange = function () {

// runs every time there is a change in the event.

}

Page 23: Ajax basics

101

xhr.onreadystatechange0 1 2 3 4 >> readyState

Page 24: Ajax basics

101

Create a callback function

xhr.onreadystatechange = function () {

if (xhr.readyState === 4){...}

}

Page 25: Ajax basics

101

Open a request

xhr.open(‘GET’, ‘sidebar.html’);

Page 26: Ajax basics

101

open(httpMethod, url, boolean)

Page 27: Ajax basics

101

HTTP GET methodReceiving or getting information from a server

https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods

Page 28: Ajax basics

101

Static and dynamic information

https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods

HTTP GET method

Page 29: Ajax basics

101

Dynamic QueryStringhttp://www.website.com/employee.php?lastName=perez

http://www.url-encode-decode.com/

Page 30: Ajax basics

101

Sending information that’s gonna be saved

https://developer.mozilla.org/en-US/docs/Web/HTTP#HTTP_request_methods

HTTP POST method

Page 31: Ajax basics

101

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

HTTP PUT method

Page 32: Ajax basics

101

Send the request

xhr.send();

Page 33: Ajax basics

101xhr.onreadystatechange = function () {

if (xhr.readyState === 4){

// Where the magic happens

}

}

xhr.open(‘GET’, ‘data/employees.json’);

xhr.send();

Page 34: Ajax basics

101

demo basic ajax

Page 35: Ajax basics

101

ResponseReply to AJAX requests

Page 36: Ajax basics

101

request

response

Browser Server

Page 37: Ajax basics

101

Simple text responseplain text or HTML

Page 38: Ajax basics

101

Request a static file

Page 39: Ajax basics

101

Lots of dataand how to handle it

Page 40: Ajax basics

101

Structured data formatXML & JSON

Page 41: Ajax basics

101

XMLTags to structure data

Page 42: Ajax basics

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>

Page 43: Ajax basics

101

JSONJavaScript Object Notation

Page 44: Ajax basics

101

JSONArray notation

Page 45: Ajax basics

101

Page 46: Ajax basics

101

[‘string’,2, true, [1,2,3,4]]

Page 47: Ajax basics

101

JSONObject notation

Page 48: Ajax basics

101

{"employees":[

{"firstName":"John", "lastName":"Doe"},

{"firstName":"Anna", "lastName":"Smith"},

{"firstName":"Peter", "lastName":"Jones"}

]}

http://jsonlint.com/

Page 49: Ajax basics

101

ParsingConvert plain text into javascript

Page 50: Ajax basics

101

demo parsing

Page 51: Ajax basics

101

Limits

Page 52: Ajax basics

101

Web Browser same origin policy

http://en.wikipedia.org/wiki/Same_origin_policy

Page 53: Ajax basics

101

Page 54: Ajax basics

101

http://www.myserver.com http://www.myserver.com/ajax.html

Browser Server

Page 55: Ajax basics

101

Page 56: Ajax basics

101

http://www.myserver.com http://www.anotherwebsite.com

Browser Server

Page 57: Ajax basics

101

http://www.myserver.com https://www.myserver.com/8888

Browser Server

Page 58: Ajax basics

101

http://www.myserver.com https://www.db.myserver.com/

Browser Server

Page 59: Ajax basics

101

JSONPJSON with padding

Page 60: Ajax basics

101

http://www.myserver.com/home

Browser

https://www.myserver.com/

Server

Page 61: Ajax basics

101

http://www.myserver.com/home

https://www.anotherserver.com/

Browser

Server

https://www.myserver.com/

Server

Page 62: Ajax basics

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>

Page 63: Ajax basics

101

Cross-Origin Resource SharingAccess-Control-Allow-Origin

Page 64: Ajax basics

101

jQueryhttp://api.jquery.com/category/ajax/shorthand-methods/

Page 65: Ajax basics

101

Load the jQuery library CDN<script src=”http://code.jquery.com/jquery-1.11.9.min.js”></script>

Page 66: Ajax basics

101

$.get();

Page 67: Ajax basics

101

$.post();

Page 68: Ajax basics

101

$.getJson();

Page 69: Ajax basics

101

demo $.load();

Page 70: Ajax basics

101

$.ajax(url, settings);http://librojquery.com/#opciones-del-método-.ajax

http://api.jquery.com/jquery.ajax/

Page 71: Ajax basics

101

$.ajax(url, settings);

var url = $(this).attr(‘action’);

Page 72: Ajax basics

101

$.ajax(url, settings);

$.ajax(url, {

data: formData,

type: ‘POST’,

success: function(response){

$(‘signup’).html(‘<p>Thank you!</p>’)

}

});

Page 73: Ajax basics

101

var url = $(this).attr(‘action’);

$.ajax(url, {

data: formData,

type: ‘POST’,

success: function(response){

$(‘signup’).html(‘<p>Thank you!</p>’)

}

});

Page 74: Ajax basics

101

$.post vs $.ajax

Page 75: Ajax basics

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>

Page 76: Ajax basics

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

Page 77: Ajax basics

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

Page 78: Ajax basics
Page 79: Ajax basics

Thank you