chapter 8 ajax & json what is ajax? ajax lets you…

64

Upload: stephany-lambert

Post on 18-Jan-2018

338 views

Category:

Documents


3 download

DESCRIPTION

WHAT IS AJAX?

TRANSCRIPT

Page 1: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
Page 2: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

CHAPTER 8

AJAX & JSON

Page 3: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

WHAT IS AJAX?

Page 4: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Ajax lets you…

Page 5: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

1

Page 6: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Request data from a server

1

Page 7: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

2Request data from a server

1

Page 8: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Request data from a server

1 2Load it without refreshing the

entire page

Page 9: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

It uses an asynchronous processing model.

(Users can do other things while the data is loading.)

Page 10: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

1THE REQUEST

The browser requests information from the server

Page 11: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

1THE REQUEST

The browser requests information from the server

ON THE SERVER

The server responds with data (usually HTML, XML, or JSON)

Page 12: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

1THE REQUEST

The browser requests information from the server

ON THE SERVER

The server responds with data (usually HTML, XML, or JSON)

2THE RESPONSE

The browser processes the content and adds it to the page

Page 13: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

REQUEST

Page 14: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Web browsers use the XMLHttpRequest object to implement Ajax functionality.

Page 15: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Here, an instance of the object is stored in a variable called xhr:

var xhr = new XMLHttpRequest;

Page 16: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The .open() method prepares the request:

var xhr = new XMLHttpRequest;xhr.open(‘GET’, ‘test.json’, true);

Page 17: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The first argument can be either HTTP GET or POST:

var xhr = new XMLHttpRequest;xhr.open(‘GET’, ‘test.json’, true);

Page 18: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The second argument specifies the file to be loaded:

var xhr = new XMLHttpRequest;xhr.open(‘GET’, ‘test.json’, true);

Page 19: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The third argument states whether the request is asynchronous or not:

var xhr = new XMLHttpRequest;xhr.open(‘GET’, ‘test.json’, true);

Page 20: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

An additional line is then written to send the request:

var xhr = new XMLHttpRequest;xhr.open(‘GET’, ‘test.json’, true);xhr.send(‘search=arduino’);

Page 21: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

RESPONSE

Page 22: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

When the server has responded, the onload event calls an anonymous function:

xhr.onload = function() { // process response};

Page 23: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

A property of the object called status is then used to make sure the data loaded okay:

xhr.onload = function() { if (xhr.status === 200) { // process response }};

Page 24: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

IE9 was the first version of IE to support this way of dealing with Ajax responses.

Page 25: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

DATA FORMATS:HTML

Page 26: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

HTML is the simplest way to get data into a page:

<div class="event"> <img src=“img/map-ny.png" alt="New York, NY” /> <p><b>New York, NY</b> <br>May 30</p></div>

Page 27: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

It is available in the responseText property of the object:

$el.innerHTML = xhr.responseText;

Page 28: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The browser renders this HTML like any other HTML - no extra work required.

Page 29: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

DATA FORMATS:XML

Page 30: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

XML looks like HTML but the tags contain different words:

<event> <location>New York, NY</location>

<date>May 15</date> <map>img/map-ny.png</map></event>

Page 31: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

It is available in the responseXML property of the object:

var events = xhr.responseXML;

Page 32: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

You need to write JavaScript to convert the XML data into HTML so it can be displayed.

Page 33: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

DATA FORMATS:JSON

Page 34: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JSON looks like object literal syntax but it is just data, not an object:

{ “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png”}

Page 35: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

It is available in the responseText property of the object:

var events = xhr.responseText;

Page 36: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

You need to write JavaScript to convert the JSON into HTML so it can be displayed.

Page 37: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JSON data is made up of keys and values:

{ “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png”}

Page 38: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JSON data is made up of keys and values:

{ “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png”}

Page 39: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The value can be a string, number, Boolean, array, object or null.

You can nest objects.

Page 40: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

{ “events”: [ { “location”: “Austin, TX”, “date”: “May 15”, “map”: “img/map-tx.png” }, { “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png” } ]}

Page 41: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

{ “events”: [ { “location”: “Austin, TX”, “date”: “May 15”, “map”: “img/map-tx.png” }, { “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png” } ]}

Page 42: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

{ “events”: [ { “location”: “Austin, TX”, “date”: “May 15”, “map”: “img/map-tx.png” }, { “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png” } ]}

Page 43: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

{ “events”: [ { “location”: “Austin, TX”, “date”: “May 15”, “map”: “img/map-tx.png” }, { “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png” } ]}

Page 44: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JavaScript has a JSON object with two important methods:

1: Convert a JavaScript object to a string:JSON.stringify();

2: Convert a string to a JavaScript object:JSON.parse();

Page 45: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JSONP

Page 46: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Ajax only works with data from the same domain. To get around this, you can use JSONP.

Page 47: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

First, a function is included in the HTML page to process the JSON data and display it on the page:

<script> function showEvents(data) { // code to process & display data }</script>

Page 48: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

Next, a <script> element calls the JSON data from a remote server:

<script> function showEvents(data) { // code to process & display data }</script>

<script src=“http://example.org/jsonp”></script>

Page 49: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The script then calls the function that was in the browser and passes the data to it as an argument:

showEvents({ “events”: [ { “location”: “New York, NY”, “date”: “May 30”, “map”: “img/map-ny.png” }... ]});

Page 50: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

JQUERY & AJAX

Page 51: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

jQuery provides methods to handle Ajax requests / responses:WORKS ON SELECTION GLOBAL METHODS OF jQuery OBJECT

.load() $.get()$.post()$.getJSON()$.getScript()$.ajax()

Page 52: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The .load() method returns the content into the jQuery selection:

$(‘#text’).load(‘ajax.html #text’);

Page 53: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The element the content will be loaded into:

$(‘#text’).load(‘ajax.html #text’);

Page 54: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The URL of the file to load comes first in the argument:

$(‘#text’).load(‘ajax.html #text’);

Page 55: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

You can specify a fragment of the page to show (not the whole page):

$(‘#text’).load(‘ajax.html #text’);

Page 56: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

The other global Ajax methods return their data in the jqxhr object.

The jqxhr object has the following properties and methods:PROPERTIES METHODSresponseText .done()responseXML .fail()status .always()statusText .abort()

Page 57: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

jQuery provides four shorthand methods to handle specific types of Ajax requests.

Page 58: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

url where the data is fetched fromdata extra information for the servercallback function to call when data returnedtype type of data to expect from server

Page 59: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

url where the data is fetched fromdata extra information for the servercallback function to call when data returnedtype type of data to expect from server

$.get(url[, data][, callback][, type])

Page 60: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

url where the data is fetched fromdata extra information for the servercallback function to call when data returnedtype type of data to expect from server

$.get(url[, data][, callback][, type])

$.post(url[, data][, callback][, type])

Page 61: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

url where the data is fetched fromdata extra information for the servercallback function to call when data returnedtype type of data to expect from server

$.get(url[, data][, callback][, type])

$.post(url[, data][, callback][, type])

$.getJSON(url[, data][, callback])

Page 62: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

url where the data is fetched fromdata extra information for the servercallback function to call when data returnedtype type of data to expect from server

$.get(url[, data][, callback][, type])

$.post(url[, data][, callback][, type])

$.getJSON(url[, data][, callback])

$.getScript(url[, callback])

Page 63: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…

There are also methods that help you deal with an Ajax reponse if it fails:

.done() when request complete

.fail() when request fails

.always() complete / fail

Page 64: CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…