Ajax Tutorial
v Asynchronous JavaScript and xml
v Browser sends and receives data in the background
v Group of technologies ,not a single technology(javascript,xml,json,html,css,php)
History of Ajax:
· 1999:microsoft’s internet explorer 5 adds xmlhttp active control
· 2000-2002:mozila creates standardized xmlhttprequest modeled on microsoft’s interface
· 2002-2006:xmlhttprequest is adopted by all web browsers
· 2004:Goole uses xmlhttprequest for gmailàuse gmail account first
· 2005:google uses uses xmlrequest for google maps
· 2004-2005:other sites begin using xmlhttprequestàammazaon,flipcart,google
· “Ajax:A new approach to web application”, Jess JamesGarrett ,Adaptive path feb 18 2005
· http:adaptive.rg/ideas/ajax-new-apporach-web-applications/
Classic HTTP Model:
Ø use performs an action(eg,clicking a link)
Ø HTTP Request sent to the server
Ø Server processes request
Ø Server returns HTML browser
Ø Users must wait for server response
Ajax Model
Ø Webpages loads javascript (“Ajax Engine”)
Ø User performs an action (eg.clicking a link”)
Ø Request guest to javascript first,not server
Ø Javascript may handle the request directly
Ø Validate data,upadate navigation,edit internal values
Ø Javascript can communicate with the server in the background
Ø Its sends xml asynchronously using xmlhttprequest
Ø Response data can be used to update the page
Ø User can continue interaction with page ,with no waiting
Ajax Components
Ø HTML,CSS,and document object model
Ø Xml
Ø Xmlhttprequest
Ø Javavascript
Ajax Responses
v ResoResponsess can either be text or XML
v responseText
v responseXML
v Text is more flexible:text,HTML,JSON,image,etc
Exmple request code:
Var xhr=new XMLHttpRequsest();
Xhr.open(“GET”,”script.php”,true);
Xhr.send();
Var text=xhr.responseText;
Var target=document.getElementById(“main”);
Target.innerHTML=text;
Parse the Response
Parse the Response
XML
JSON
Javascript and JSON Datatypes:
Var person={
‘fisr_name’:’Bob’,
‘last_name’:’smith’
},
Var response=”{
‘first_name’:bob’’,
‘last_name’:’smith’
}”;
Var person=JSON.parse(reponse);
Ready States:
v 0:connection created but not opened
v 1:Connection opened
v Request sent,received by server
v Response in progress(partial data)
v Response complete (success or failure)
Ready State Change
ü onreadystatechange
ü Used to store a javascript functiom
ü Called each time readystate changes
ü Prevents having to constantly recheck value
Onreadystayechange function:
Var xhr= new XMLHttpRequest();
Xhr.open(“GET”,”script.php”,true);
Xhr.onreadystatechange=function(){
If(xhr.readyState==4&&xhr.status==200){
Var target=document.getElementById(“main”);
Target.innerHTML=xhr.responseTexe;
}}
This post first appeared on WebDesign | SEO Services Full Tutorial | PHP Full Tutorial | Javascript Full Tutorial|HTML,CSS,CMS, please read the originial post: here