11 introducing ajax

17
INTRODUCING JQUERY AJAX The easy way to do Ajax

Transcript of 11 introducing ajax

Page 1: 11 introducing ajax

INTRODUCING JQUERY AJAX The easy way to do Ajax

Page 2: 11 introducing ajax

Processing client-side is much more satisfying and efficient

Page 3: 11 introducing ajax

Ajax is required to make things work client-side • Clearly we need server-side processing to persist things

to a database

Page 4: 11 introducing ajax

But Ajax has a lot of moving parts • XMLHttpRequest, callbacks, readyState, status, etc., etc.

Page 5: 11 introducing ajax

There are several ways to process Ajax with jQuery •  $('selector').load() •  $.get() •  $.post() •  $.getJSON() •  $.postJSON() •  $.ajax()

Page 6: 11 introducing ajax

The ajax() call is the most complex •  $.ajax(settingsJSONObject); •  For example: $.ajax({!type: 'DELETE',!url: "Greendale/students/",!success: function() { alert('Deleted'); },!error: function() { alert('Oh noes!'); },!data: {! studentId: 334234,! name:'Annie Edison', ! age:'21', ! major:'undecided'},!timeout: 60000!});!

Page 7: 11 introducing ajax

The load() call is the simplest • Calls a service and loads the response into the selector. • Response is expected to be HTML $('selector').load(url);!

Page 8: 11 introducing ajax

$.get() and $.post() •  The most common methods •  Identical except the method they use to call the service •  To send an http GET request to a uri $.get(url);!•  To send an http POST request to a uri $.post(url);!

Page 9: 11 introducing ajax

You usually have to send data to the server $.post(url, data);!• Querystring var data = 'firstName=Abed&lastName=Nadir&school=Greendale%20Community%20College';!•  JSON var data = {! firstName: 'Abed',! lastName: 'Nadir',! school: 'Greendale Community College'!};!

Page 10: 11 introducing ajax

The serialize() function can help with preparing data to send to the server • Writing the code to convert a form with many fields is

time-consuming. Do this instead: var jsonForm = $('theForm').serialize();!

Page 11: 11 introducing ajax

The callback function is used to process data returning from the server • Data returns in XML or JSON format $.get(url, sendData, function (data, status) {! // Do something with the response here!});!•  status can be one of

•  success!•  timeout!•  error!•  notmodified!•  parsererror!

Page 12: 11 introducing ajax

The error function handles any errors returned from the server •  $.get(url, data, successful).error(errorFunction); • Without the error function, the get and post fail silently

Page 13: 11 introducing ajax

JSON is simply a way to record ad-hoc JavaScript objects • You don't need to create a class to have objects. •  Just declare them like so: var student1 = {! firstName: Abed, lastName: Nadir,! school: 'Greendale Community College'!};!• Rules:

•  Free-form layout (line breaks don't matter) •  Bounded by hitchcocks •  Comma-separated •  Strings must be quoted to preserve whitespace

Page 14: 11 introducing ajax

There are two main ways to process JSON from an Ajax call • Converting the string to JSON manually $.get(url, dataIn, function (data) {! var jsondata = JSON.parse(data);! // Do something with the JSON object!});!• Using $.getJSON() and $.postJSON() $.getJSON(url, dataIn, function (data) {! // Do something with the JSON object!});!• Same exactly except that $.getJSON() and $.postJSON()

expect a JSON object to be returned

Page 15: 11 introducing ajax

You can read JSON data using the dotted notation var name = student1.firstName + " " +! student1.lastName;!

Page 16: 11 introducing ajax

JSON objects often contain other JSON objects

var students = {! student1: {! firstName: Abed, ! lastName: Nadir,! school: 'Greendale Community College'! },! student2: {! firstName: Troy, ! lastName: Barnes,! school: 'Greendale Community College'! }!};!

Page 17: 11 introducing ajax

Conclusion •  There are several ways to make Ajax calls with jQuery:

•  $('selector').load(url); •  $.get(url); •  $.post(url); •  $.getJSON(url); •  $.postJSON(url); •  $.ajax();

• You send data with these functions in QueryString or JSON format.

• Serialize helps to convert forms data into JSON •  load() populates a selector with html •  $.get() and $.post() are the most common •  $.getJSON() and $.postJSON() expect to get JSON back