chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · title: powerpoint...
TRANSCRIPT
![Page 1: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/1.jpg)
Chapter 14
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 1
How to use Ajax, JSON, and Blogger
![Page 2: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/2.jpg)
Objectives
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 2
Applied
1. Use Ajax with HTML, XML, or JSON data to update a web page
without reloading it.
2. Use Ajax and JSON with the API for Google Blogger to get data
from a blog and display it on a web page.
3. Use an online JSON editor to review the feed from a web site.
Knowledge
1. In general terms, describe the way Ajax works, including the use of
the XMLHttpRequest object.
2. In general terms, distinguish between XML and JSON data.
3. Distinguish between these jQuery methods for making Ajax requests:
load, $.get, $.post, $.getJSON, and $.ajax.
4. Describe JSONP and cross-domain security policies.
![Page 3: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/3.jpg)
Objectives (continued)
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 3
5. In general terms, describe the way you use the API for a web site to
get data from the web site and display it on your web pages.
6. In general terms, describe how an online JSON editor can help you
understand the data items in a feed from a web site.
![Page 4: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/4.jpg)
Google’s Auto Suggest is an Ajax application
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 4
![Page 5: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/5.jpg)
How a normal HTTP request is processed
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 5
Application Server
(Scripts)
Web Server Database Server
HTTP Request
HTTP Response
(new page)`
Web Browser
![Page 6: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/6.jpg)
How an Ajax XMLHttpRequest is processed
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 6
Application Server
(Scripts)
Web Server Database Server
XMLHttpRequest
Response
(Data only)`
Web Browser
(JavaScript)
Terms
Ajax (Asynchronous JavaScript and XML
XMLHttpRequest (XHR) object
API (Application Programming Interfaces)
![Page 7: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/7.jpg)
The common data formats for Ajax
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 7
Format Description Extension
HTML Hypertext Markup Language html
XML eXtensible Markup Language xml
JSON JavaScript Object Notation json is often used
![Page 8: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/8.jpg)
XML data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 8
<?xml version="1.0" encoding="utf-8"?>
<management>
<teammember>
<name>Agnes</name>
<title>Vice President of Accounting</title>
<bio>With over 14 years of public accounting ...
</bio>
</teammember>
<teammember>
<name>Wilbur</name>
<title>Founder and CEO</title>
<bio>While Wilbur is the founder and CEO ... </bio>
</teammember>
</management>
![Page 9: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/9.jpg)
JSON data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 9
{"teammembers":[
{
"name":"Agnes",
"title":"Vice President of Accounting",
"bio":"With over 14 years of public accounting ..."
},
{
"name":"Wilbur",
"title":"Founder and CEO",
"bio":"While Wilbur is the founder and CEO ..."
}
]}
![Page 10: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/10.jpg)
Terms
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 10
XML (eXtensible Markup Language)
JSON (JavaScript Object Notation)
![Page 11: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/11.jpg)
Members of the XMLHttpRequest object
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 11
Methods
abort()
getAllResponseHeaders()
getResponseHeader(name)
open(method,url[,async][,user][,pass])
send([data])
setRequestHeader(name,value)
Properties
readyState
responseText
responseXml
status
statusText
Event
onreadystatechange
![Page 12: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/12.jpg)
A web page that uses the XHR object
and JavaScript to load XML data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 12
The HTML div element that receives the data <div id="team"></div>
![Page 13: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/13.jpg)
The XML file (team.xml)
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 13
<?xml version="1.0" encoding="utf-8"?>
<management>
<teammember>
<name>Agnes</name>
<title>Vice President of Accounting</title>
<bio>With over 14 years of public accounting ...
</bio>
</teammember>
...
</management>
![Page 14: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/14.jpg)
The JavaScript for getting and parsing the data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 14
$(document).ready(function() {
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
xmlDoc = xhr.responseXML;
var team =
xmlDoc.getElementsByTagName("teammember");
var html = "";
for (i = 0; i < team.length; i++) {
html +=
xmlDoc.getElementsByTagName(
"name")[i].childNodes[0].nodeValue + <br>" +
xmlDoc.getElementsByTagName(
"title")[i] .childNodes[0].nodeValue +
"<br>" +
xmlDoc.getElementsByTagName(
"bio")[i] .childNodes[0].nodeValue +
"<br><br>"; }
document.getElementById("team").innerHTML = html;
}
}
xhr.open("GET", "team.xml", true);
xhr.send();
});
![Page 15: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/15.jpg)
The methods for working with Ajax
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 15
The shorthand methods
load(url[,data][,success])
$.get(url[,data][,success[,dataType])
$.post(url[,data][,success[,dataType])
$.getJSON(url[,data][,success])
The $.each method for processing returned data
$.each(collection, callback)
![Page 16: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/16.jpg)
Examples of Ajax methods
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 16
A load method
$("#solution").load("solutions.html");
A $.get method
$.get("getmanager.php", "name=agnes", showManager);
A $.getJSON method
$.getJSON("team.json", function(data){
// the statements for the success function
}
![Page 17: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/17.jpg)
A web page that loads HTML elements
when a link is clicked
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 17
The HTML for the user Interface <p>Which Vecta Corp. solution are you interested in
learning about?</p>
<a id="vprospect" href="#">vProspect 2.0</a> |
<a id="vconvert" href="#">vConvert 2.0</a> |
<a id="vretain" href="#">vRetain 1.0</a><br>
<div id="solution"></div>
![Page 18: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/18.jpg)
The start of the second div element
in the solutions.html file
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 18
<div id="vconvert">
<p><strong>vConvert 2.0</strong></p>
<p><img src="images/logo_vconvert.gif"
width="63" height="36" >
Create a highly user-friendly ...
</p>
<ul>
<li>Build a visual and functional user ... </li>
<li>Cause the desired emotional response ...</li>
...
</ul>
</div>
![Page 19: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/19.jpg)
The jQuery that loads the data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 19
$(document).ready(function() {
$("#vprospect").click(function() {
$("#solution").load("solutions.html #vprospect");
});
$("#vconvert").click(function() {
$("#solution").load("solutions.html #vconvert");
});
$("#vretain").click(function() {
$("#solution").load("solutions.html #vretain");
});
});
![Page 20: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/20.jpg)
A web page that loads XML data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 20
The HTML div element that receives the data <div id="team"></div>
![Page 21: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/21.jpg)
The XML file (team.xml)
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 21
<management>
<teammember>
<name>Agnes</name>
<title>Vice President of Accounting</title>
<bio>With over 14 years of public accounting ...
</bio>
</teammember>
...
</management>
![Page 22: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/22.jpg)
The jQuery
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 22
$(document).ready(function(){
$.get("team.xml", function(data){
$("#team").html("");
$(data).find("management").children().each(function() {
var xmlDoc = $(this);
$("#team").append("<h3>" +
xmlDoc.find("name").text() + "</h3>" +
xmlDoc.find("title").text() + "<br>" +
xmlDoc.find("bio").text() + "<br>");
});
});
});
![Page 23: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/23.jpg)
A web page that loads JSON data
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 23
The HTML div element that receives the data <div id="team"></div>
![Page 24: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/24.jpg)
The JSON file (team.json)
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 24
{"teammembers":[
{
"name":"Agnes",
"title":"Vice President of Accounting",
"bio":"With over 14 years of public accounting...
},
{
"name":"Damon",
"title":"Director of Development",
"bio":"Damon is the Director of Development ... "
}
]}
![Page 25: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/25.jpg)
The jQuery
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 25
$(document).ready(function(){
$.getJSON("team.json", function(data){
$.each(data, function() {
$.each(this, function(key, value) {
$("#team").append(
"Name: " + value.name + "<br>" +
"Title: " + value.title + "<br>" +
"Bio: " + value.bio + "<br><br>"
);
});
});
});
});
![Page 26: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/26.jpg)
Two ways to send data with an Ajax request
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 26
A $.get method that uses a string for the data parameter
$(document).ready(function() {
$.get("getmanager.php", "name=wilbur", showManager);
function showManager(data) {
// process data
}
});
A $.get method that uses a map for the data parameter
$(document).ready(function() {
$.get("getmanager.php", {name:wilbur}, showManager);
function showManager(data) {
// process data
}
});
![Page 27: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/27.jpg)
The helper methods for working with Ajax
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 27
serialize()
serializeArray()
The HTML for a form
<form id="contactForm">
<!-- the controls for the form -->
</form>
jQuery that uses the serialize method
$(document).ready(function() {
var formData = $("#contactForm").serialize();
$.get("processcontact.php", formData,
processReturnedData);
function processReturnedData(data) {
// the statements for the success function
}
});
![Page 28: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/28.jpg)
The syntax of the $.ajax method
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 28
$.ajax({ options })
Some of the options for the $.ajax method url
beforeSend(jqXHR, settings)
cache
complete(jqXHR, status)
data
dataType
error(jqXHR, status, error)
jsonp
password
success(data, status, jqXHR)
timeout
type
username
![Page 29: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/29.jpg)
Terms
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 29
jqXHR object
JSONP, or “JSON with padding”
![Page 30: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/30.jpg)
A web page with a loading message
and an alert dialog box for errors
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 30
The HTML div element that receives the data <div id="team"></div>
![Page 31: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/31.jpg)
The XML file
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 31
<management>
<teammember>
<name>Agnes</name>
<title>Vice President of Accounting</title>
<bio>With over 14 years of public accounting ...
</bio>
</teammember>
...
</management>
![Page 32: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/32.jpg)
The jQuery for the $.ajax method
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 32
$.ajax({
type: "get",
url: "team.xml",
beforeSend: function() {
$("#team").html("Loading...");},
timeout: 10000,
error: function(xhr, status, error) {
alert("Error: " + xhr.status + " - " + error);
dataType: "xml",
success: function(data) {
$("#team").html("");
$(data).find(
"management").children().each(function() {
var xmlDoc = $(this);
$("#team").append("<h3>" +
xmlDoc.find("name").text() + "</h3>" +
xmlDoc.find("title").text() + "<br>" +
xmlDoc.find("bio").text() + "<br>");
});
}
});
![Page 33: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/33.jpg)
A fictitious blog on Google’s Blogger
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 33
![Page 34: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/34.jpg)
Google URLs
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 34
The URL for Google’s Blogger
www.blogger.com
The URL for Google APIs
https://code.google.com/apis/console/
![Page 35: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/35.jpg)
The API documentation for Blogger
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 35
The URL for the API documentation (one line) https://developers.google.com/blogger/docs/3.0/
getting_started
![Page 36: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/36.jpg)
The format of a URL for getting posts (one line)
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 36
https://www.googleapis.com/blogger/v3/blogs/
BLOG-ID/posts?key=API-KEY
Some of the data items for working with posts published
updated
url
title
content
author.id
author.displayName
author.url
author.image
replies.totalItems
![Page 37: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/37.jpg)
The Vecta Corp blog in an online JSON editor
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 37
![Page 38: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/38.jpg)
The URL for the online JSON editor
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 38
www.jsoneditoronline.org
The URL for the Vecta Corp blog (one line) www.googleapis.com/blogger/v2/blogs/8698899267502010836/
posts?key=AIzaSyByJiL407voPRmQyNAYZKWxBC5FuIlBIdQ
![Page 39: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/39.jpg)
How to review a feed in the online JSON editor
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 39
1. Type or paste the URL for the feed into the address bar of your
browser, and press the Enter key to see the feed in the browser.
2. Copy all of the JSON, go to the URL for the online JSON editor, and
paste the JSON into the left pane of the editor.
3. Click the Format button at the top of the left pane to see the formatted
JSON data.
4. Click the right arrow button between the panes to see a structured
version of the data.
5. Click on the Expand All button at the top of the right pane to see all
of the data in the feed.
![Page 40: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/40.jpg)
A web page that displays Blogger posts
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 40
The HTML div element that receives the blog <div id="posts"></div>
![Page 41: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/41.jpg)
The jQuery for using Blogger
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 41
var apikey = ""; // set this to your API key
var html = "";
$.support.cors = true;
$(document).ready(function() {
$.ajax({
// the number in the url is the id for the blog
url:"https://www.googleapis.com/blogger/v2/blogs/
8698899267502010836/posts?key=" + apikey,
dataType: "json",
success: function(data) {
$.each(data.items, function(index, value) {
html += "<h3><a href='" + value.url +
"'>" + value.title + "</a></h3>" +
value.content +
"<br>Posted by <a href='" +
value.author.url + "'>" +
value.author.displayName + "</a>";
}); // end each method
$("#posts").html(html);
} // end ajax options
}); }); // end ajax and ready methods
![Page 42: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/42.jpg)
Exercise 14-1: Modify the Blogger application
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 42
Add the publication date and number of replies to the information that’s displayed.
![Page 43: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/43.jpg)
Extra 14-1: Convert an Ajax application
from XML to JSON
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 43
The same application as in figure 14-11, but using JSON data instead of XML data.
![Page 44: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/44.jpg)
Extra 14-2: Enhance an Ajax application
so it uses an expanded JSON file
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 44
![Page 45: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/45.jpg)
Extra 14-3: Load speakers as they’re requested
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 45
When the user clicks a link, the JSON for the page should be loaded from its file.
![Page 46: Chapter 14zolinsoft.com/cis1730/murach.powerpoint.pdfs/chapter14slides.pdf · Title: PowerPoint Presentation Author: Maria David Created Date: 3/25/2015 3:24:45 PM](https://reader036.vdocument.in/reader036/viewer/2022071511/6130bbe61ecc515869444925/html5/thumbnails/46.jpg)
Short 14-1: Convert a $.getJSON method to $.ajax
Murach's JavaScript and jQuery, C14 © 2012, Mike Murach & Associates, Inc. Slide 46
Estimated time: 10 to 15 minutes.