Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15-437 Ajax & Json

ThierrySans
September 29, 2013

15-437 Ajax & Json

ThierrySans

September 29, 2013
Tweet

More Decks by ThierrySans

Other Decks in Education

Transcript

  1. Why do we need Ajax? So far, when we wanted

    to • send data to the server • or retrieve data from the server ๏ we had to refresh the entire page 
 (i.e reloading HTML, CSS, JS and all media files) ✓ But, why not using Javascript to process the data 
 and perform the necessary page changes?
  2. Ajax - Asynchronous Javascript And XML Fetch/push content from/to the

    server asynchronously 
 i.e without having to refresh the page ๏ Ajax is not a language ✓ It is a simple Javascript command

  3. History of Ajax • Patent from Microsoft (filled in 2000,

    granted in 2006) • XMLHTTP ActiveX control (Internet Explorer 5) • Adopted and adapted by Opera, Mozilla and Apple • XMLHttpRequest Javascript object (standard) • Before / After IE7 ๏ Different code for different browser (emergence of the javascript framework Prototype) ✓ Javascript Object was adopted by IE7
  4. Ajax revolutionized the Web ✓ Started with Gmail and Google

    Maps • Advantages • Low latency • Rich interactions • Consequences • Webapp center of gravity moved to the client side • Javascript engine performance race
  5. Ajax made simple using jQuery for reference see http://api.jquery.com/category/ajax/ $.ajax({!

    type: "POST",! url: "www.example.com/resource/",! data: "first=John&last=Doe",! success: function(data){! $("#results").append(data);! }! }); $.get and $.post are shortcut methods defined on $.ajax HTTP method URL requested Arguments Callback method to process the result
  6. “Hello You !” /?name=CMU “Hello CMU!” Controller def sayHelloGet(request):! name

    = request.GET['name']! return HttpResponse("Hello %s!" % name) function doAjax(){! var name = $("#name").val();! $.get('sayhelloget/', ! {name: name} ,! function(data) {! $('#result').html(data);! });! } Callback method onsuccess
  7. Callback methods beforeSend before sending the request error when the

    request fails success when the request succeed dataFilter when data are received (before success) complete when the request finishes whether in failure or success Warning: Callback methods are executed in a child thread
  8. main thread child thread Concurrency issue in Ajax - a

    typical example function doAjax(){! var name = $("#name").val()! var result = “”! $.get('sayhelloget/', ! {name: name} ,! function(data) {! result = data! });! $('#result').html(result);! } initialization assignment access result will either be “” or “Hello CMU”! depending on the execution (non determinism) ➡ Race Condition!
  9. Sending structured data How to send a structured data (arrays

    or dictionaries) through an HTTP request or response? ➡ Only strings are send back and forth ✓ Have a string representation of a complex data structure function sendArray(){! var arr = [“1”,”2”,”3”]! $.get('/savearray/', {arg: arr}, ... function saveArray(){! arr = request.GET[‘arg’] GET /arg=??? Javascript object array Python object array must be a string!
  10. Why do we need JSON? Original idea: using XML ✓

    In practice: JSON is used for its simplicity
  11. The JSON standard (RFC 4627) • Lightweight open format to

    interchange data • Human readable • Used for serializing and transmitting structured data over a network connection (HTTP mostly) • Since 2009 browsers support JSON natively source http://en.wikipedia.org/wiki/JSON
  12. Anatomy of JSON • A JSON data structure is either

    array (indexed array) object (associative array)
 • JSON values are
 string - number - object - array - true - false - null
  13. JSON Array image from http://www.json.org/ [ {"name": "Thierry"}, {"name": "Jeff"},

    {"name": "Bill"}, {"name": "Mark"}, ] [1, 2, 3, 4, 5] or
  14. JSON Object image from http://www.json.org/ { "firstName": "John", "lastName": "Smith",

    "age": 25, "male": true "address": { "streetAddress": "21 2nd Street", "additionalAddress": null "city": "New York", "state": "NY", "postalCode": "10021" } }
  15. Serialization - Deserialization ! Javascript array GET /arg=”[... ! JSON

    array response ! JSON array ! Javascript array ! Python array ! JSON array ! Python array ! JSON array ... Serialization Deserialization
  16. JSON in Javascript (natively supported) Serialization Javascript JSON ! !

    Deserialization Javascript JSON var myObject = JSON.parse(myJSONtext) var myJSONText = JSON.stringify(myObject);
  17. JSON in Python (native Python library) Serialization Python JSON !

    ! Deserialization Python JSON myObject = json.loads(myJSONtext) myJSONText = json.dumps(myObject); import json