Академический Документы
Профессиональный Документы
Культура Документы
Introduction to Ajax
Ajax is the acronym for Asynchronous JavaScript and XML
All moderns browsers provide an XMLHttpRequest object that is used to send an Ajax request to the web server and to receive the returned data from the server Ajax lets you receive data from a web server without reloading the page partial page refresh
`
Web Browser
Web Server
Database Server
Server returns an entire page for a normal HTTP request so the page has to be loaded into the browser
Ajax XMLHttpRequest
How an Ajax XMLHttpRequest is processed
XMLHttpRequest
`
Web Browser (JavaScript)
Web Server
Database Server
An Ajax request sends an XMLHttpRequest object to get data and the server returns only the data JavaScript is used to send the request, process the returned data and script the DOM with the new data
Common Formats
Format HTML XML JSON Description Hypertext Markup Language eXtensible Markup Language JavaScript Object Notation File extension html xml json
XML is an open-standard, device-independent format that must be parsed by JavaScript or jQuery in the browser For the most part, JSON files are smaller and faster than XML
XML Data
<?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>
JSON Data
{"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 ..." } ]}
XMLHttpRequest Object
Method abort() open(method,ur l[,async][,use r][,pass]) send([data]) Property readyState responseText Event onreadystate An event that occurs when the state of the request changes. A numeric value that indicates the state of the current request: 0 is UNSENT, 1 is OPENED, 2 is HEADERS_RECIEVED, 3 is LOADING and 4 is DONE The content thats returned from the server in plain text format. Description Cancels the current request. Opens a connection for the request. The parameters let you set the method to GET or POST, set the URL for the request, set asynchronous mode to true or false and supply a username and password if authentication is required. When asynchronous is used, the application continues while the request is being processed. Starts the request. This method can include data that gets sent with the request. This method must be called after a request connection has been opened.
Description Load HTML data. Load data with GET response. Load data with a POST request. Load JSON data with a GET request.
Description The string for the URL to which the request is sent. A map or string that is sent to the server with the request, usually to filter the data that is returned. A function that is executed if the request is successful. A string that specifies the type of data (html, xml, json, script or text). The default is XML.
A $.get method
$ .get(getmanager.php, name=agnes, showManager);
A $.getJSON method
$.getJSON(team.json, function(data){ // the statements for the success function }
<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>
The jQuery
$(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>"); }); }); });
<div id=posts></div>
Blogger (cont)
jQuery for Using Blogger
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