On Github AnnieCannons / ac-intro-to-ajax
How do JavaScript and HTML work together?
Static Content and Progressive Enhancement Dynamic ContentThis also provides an enhanced version of the page to those with more advanced browser software or greater bandwidth.
Adding Data (often user data)!
JSON = JavaScript Object Notation
{
"firstName": "Jane",
"lastName": "Smith",
"address": {
"streetAddress": "425 2nd Street",
"city": "San Francisco",
"state": "CA",
"postalCode": 94107
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567" ]
}
var myProfile =
{"firstName": "Liz",
"lastName": "Howard",
"cats": ["Tribbles", "Jean Claws"]};
var p = document.createElement('p');
p.innerHTML = 'My name is ' +
myProfile.firstName + ' ' + myProfile.lastName + '. ';
p.innerHTML += 'My cats are ' +
myProfile.cats.join(', ') + '.';
var p = $('<p>');
p.html('My name is ' + myProfile.firstName + ' ' +
myProfile.lastName + '. ');
p.append('My cats are ' + myProfile.cats.join(', ') + '.');
Follow this link
Asynchronous technologies enable users to access information or communicate at different points of time, usually at the time of choice of the user.
The traditional data-driven web application is written so that it:
Displays a webpage Waits for user interaction Asks the server for data Reloads the webpageThis has the effect that the user has "down time" - there is a period of time where they can't interact with the webpage at all
AJAX = "Asynchronous JavaScript and XML."
What else?
XMLHttpRequest (XHR) is an API (woohoo!) available to web browser scripting languages such as JavaScript. It is used to send HTTP or HTTPS requests to a web server and load the server response data back into the script.
// instantiate a new request
var request = new XMLHttpRequest();
// add event listeners
request.addEventListener('load', function () {
// transform a string into a usable object
console.log(JSON.parse(request.responseText));
});
request.open('get', '/path/to/api', true);
request.setRequestHeader('Content-type', 'application/json');
request.send();
We can use the jQuery $.ajax function.
$.ajax({type: "GET",
url: "filename.json",
dataType: "json",
success: function(data) { },
error: function(xhr, status, e) { }
});
$.ajax({type: "GET",
url: "books.json",
dataType: "json",
success: function(books) {
for (var i = 0; i < books.length; i++) {
var book = books[i];
var p = $('<p>');
p.html(book.title + ' by ' + book.author);
$('body').append(p);
}
},
error: function(xhr, status, e) {
console.log(status, e);
}
});
jQuery is just a library, written by human beings. Use it because it saves you time, but don't rely too hard on it!
Follow this link
Looking at "GET" and "POST"
$.ajax({type: "GET",
url: "/api/books",
dataType: "json",
data: {'sortBy': 'author'},
success: processBooks
});
$.ajax({type: "POST",
url: "/api/book/new",
dataType: "json",
data: {'title': 'I, Robot', 'author': 'I. Asimov'}
success: processBoooks
});
For http://store.company.com/dir/page.html:
We will check out this example using Meetup's API Click here
(Let's add the Chrome extension JSON formatter)
$.ajax({
type:"GET",
url:"https://api.meetup.com/2/cities",
success: function(data) {
$('.text').html('');
for (var i = 0; i < data.results.length; i++){
var place = data.results[i].city + "," +
data.results[i].state;
$(".text").append("" + place + "
"): }},What happens?
Thank you for your attention!