SlideShare a Scribd company logo
1 of 27
AJAX – Part IIProgramming in Ajax Mukesh N. Tekwani tekwani@email.com
Programming in Ajax We consider a program that asks the user to click a button, fetches data from the server using Ajax techniques, and displays that data in the same Web page as the button – without refreshing the page. November 25, 2010 2 Mukesh N Tekwani, Mumbai
Programming in Ajax November 25, 2010 3 Mukesh N Tekwani, Mumbai
Programming in Ajax November 25, 2010 4 Mukesh N Tekwani, Mumbai
<body> <h1>Fetching data with Ajax</h1> <form> <input type = "button" value = "Display 	Message"  		onclick = 	"getData('http://localhost/AJAX/data.txt', 	'targetDiv')">  </form> <div id = "targetDiv"> <p>The data fetched will go here</p> </div> </body> November 25, 2010 5 Mukesh N Tekwani, Mumbai
The body of the page starts by displaying the original text in a <div> element 	<div id = "targetDiv"> 		<p>The data fetched will go here</p> 	</div> November 25, 2010 6 Mukesh N Tekwani, Mumbai
There’s a button on this page. When the user clicks on the button, a JavaScript method named getData is called.    <form> 		<input type = "button"  		value = "Display Message"  		onclick = 	"getData('http://localhost/AJAX/data.txt','targetDiv')">  </form> The getData function is passed two strings: November 25, 2010 7 Mukesh N Tekwani, Mumbai
The first string is: 	onclick = "getData('http://localhost/AJAX/data.txt','targetDiv')">  The getData function is passed two strings: The name of the text file data.txt to fetch from the server, and The name of the <div> element to display the fetched text in. November 25, 2010 8 Mukesh N Tekwani, Mumbai
Creating the XMLHttpRequest Object The code to create the XMLHttpRequest object is outside any function, so this code runs immediately as the page loads. We create a variable for this object  XMLHttpRequestObject. var XMLHttpRequestObject = false; 	This variable is initialized to the value ‘false’ so that the script can check later whether the variable was created. November 25, 2010 9 Mukesh N Tekwani, Mumbai
XMLHttpRequestObject The XMLHttpRequest object is part of the browser’s window object. So to check whether the XMLHttpRequest object is ready to use, we use the if statement. If XMLHttpRequest object is available, we can create the object as follows: 	if(window.XMLHttpRequest) 	{ 		XMLHttpRequestObject = new XMLHttpRequest(); 	} November 25, 2010 10 Mukesh N Tekwani, Mumbai
XMLHttpRequestObject If we are working with Internet Explorer, we can create an XMLHttpRequest object like this: 	else if (window.ActiveXObject) 	{ 		XMLHttpRequestObject = new 	ActiveXObject("Microsoft.XMLHTTP");	 	} November 25, 2010 11 Mukesh N Tekwani, Mumbai
XMLHttpRequestObject Now we have an XMLHttpRequest object. The properties and methods of the XMLHttpRequest object for IE are as follows: November 25, 2010 12 Mukesh N Tekwani, Mumbai
XMLHttpRequestObject onreadystatechange – holds the name of the event handler that should be called when the value of the readystate property changes. In our example, it is function() readyState – holds the state of the request November 25, 2010 13 Mukesh N Tekwani, Mumbai
Checking to make sure we have the XMLHttpRequestObject The getData() function will be used to actually fetch the text data from the file We first check that there is a valid object with the statement if(XMLHttpRequestObject) If the object creation hadn’t worked, then this variable will hold a value ‘false’ and the condition would become false. November 25, 2010 14 Mukesh N Tekwani, Mumbai
Opening the XMLHttpRequestObject ,[object Object]
This object has a method called “open()”. We will use that method to use the URL we want.
Syntax of open() method is:
open (“method”, “URL”, [,asyncFlag [, “username” [, “password”]]])November 25, 2010 15 Mukesh N Tekwani, Mumbai
Opening the XMLHttpRequestObject ,[object Object]
open (“method”, “URL”, [,asyncFlag [, “username” [, “password”]]])
method – the HTTP method used to open the connection such as GET, POST, PUT, HEAD
URL – the requested URL
asyncFlag – A boolean value indicating whether the call is asynchronous or synchronous. Default is ‘true’ (asynchronous)November 25, 2010 16 Mukesh N Tekwani, Mumbai
Opening the XMLHttpRequestObject ,[object Object]
To open a URL we can use the GET, POST or PUT methods.
But in Ajax, we will use GET to retrieve data and POST to send a lot of data to the serverXMLHttpRequestObject.open("GET", dataSource); November 25, 2010 17 Mukesh N Tekwani, Mumbai
Opening the XMLHttpRequestObject ,[object Object]

More Related Content

What's hot

jQuery basics for Beginners
jQuery basics for BeginnersjQuery basics for Beginners
jQuery basics for BeginnersPooja Saxena
 
Ajax Fundamentals Web Applications
Ajax Fundamentals Web ApplicationsAjax Fundamentals Web Applications
Ajax Fundamentals Web Applicationsdominion
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11Smita B Kumar
 
Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"Fwdays
 
Dot net technology
Dot net technologyDot net technology
Dot net technologyMilanMiyani1
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsAleksandar Ilić
 
09 Application Design
09 Application Design09 Application Design
09 Application DesignRanjan Kumar
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its managementVinay Kumar Pulabaigari
 
Deceptive simplicity of async and await
Deceptive simplicity of async and awaitDeceptive simplicity of async and await
Deceptive simplicity of async and awaitAndrei Marukovich
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applicationsAlex Golesh
 
Introduction to AJAX and DWR
Introduction to AJAX and DWRIntroduction to AJAX and DWR
Introduction to AJAX and DWRSweNz FixEd
 

What's hot (17)

AJAX
AJAXAJAX
AJAX
 
jQuery basics for Beginners
jQuery basics for BeginnersjQuery basics for Beginners
jQuery basics for Beginners
 
Ajax Fundamentals Web Applications
Ajax Fundamentals Web ApplicationsAjax Fundamentals Web Applications
Ajax Fundamentals Web Applications
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11
 
Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"
 
Workflow Foundation 4
Workflow Foundation 4Workflow Foundation 4
Workflow Foundation 4
 
Javascript talk
Javascript talkJavascript talk
Javascript talk
 
Understanding AJAX
Understanding AJAXUnderstanding AJAX
Understanding AJAX
 
mediator
mediatormediator
mediator
 
Dot net technology
Dot net technologyDot net technology
Dot net technology
 
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App ComponentsJava Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
 
09 Application Design
09 Application Design09 Application Design
09 Application Design
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
 
Deceptive simplicity of async and await
Deceptive simplicity of async and awaitDeceptive simplicity of async and await
Deceptive simplicity of async and await
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Windows 8 metro applications
Windows 8 metro applicationsWindows 8 metro applications
Windows 8 metro applications
 
Introduction to AJAX and DWR
Introduction to AJAX and DWRIntroduction to AJAX and DWR
Introduction to AJAX and DWR
 

Viewers also liked

Viewers also liked (11)

Ajax part i
Ajax part iAjax part i
Ajax part i
 
Dom
DomDom
Dom
 
Coding Ajax
Coding AjaxCoding Ajax
Coding Ajax
 
Ajax chap 2.-part 1
Ajax chap 2.-part 1Ajax chap 2.-part 1
Ajax chap 2.-part 1
 
Ajax chap 3
Ajax chap 3Ajax chap 3
Ajax chap 3
 
S.E. DBMS-Paper Analysis
S.E. DBMS-Paper AnalysisS.E. DBMS-Paper Analysis
S.E. DBMS-Paper Analysis
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
XML
XMLXML
XML
 
OSI Model - Every Detail Explained
OSI Model - Every Detail ExplainedOSI Model - Every Detail Explained
OSI Model - Every Detail Explained
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
OSI Model of Networking
OSI Model of NetworkingOSI Model of Networking
OSI Model of Networking
 

Similar to Ajax Part II (20)

Ajax
AjaxAjax
Ajax
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Ajax Ppt
Ajax PptAjax Ppt
Ajax Ppt
 
AJAX
AJAXAJAX
AJAX
 
AJAX
AJAXAJAX
AJAX
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Core Java tutorial at Unit Nexus
Core Java tutorial at Unit NexusCore Java tutorial at Unit Nexus
Core Java tutorial at Unit Nexus
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Ajax
AjaxAjax
Ajax
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Ajax
AjaxAjax
Ajax
 
Ajax Tuturial
Ajax TuturialAjax Tuturial
Ajax Tuturial
 
Pascarello_Investigating JavaScript and Ajax Security
Pascarello_Investigating JavaScript and Ajax SecurityPascarello_Investigating JavaScript and Ajax Security
Pascarello_Investigating JavaScript and Ajax Security
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
jQuery : Talk to server with Ajax
jQuery : Talk to server with AjaxjQuery : Talk to server with Ajax
jQuery : Talk to server with Ajax
 
Ajax for dummies, and not only.
Ajax for dummies, and not only.Ajax for dummies, and not only.
Ajax for dummies, and not only.
 

More from Mukesh Tekwani

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelMukesh Tekwani
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfMukesh Tekwani
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 

More from Mukesh Tekwani (20)

Computer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube ChannelComputer Science Made Easy - Youtube Channel
Computer Science Made Easy - Youtube Channel
 
The Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdfThe Elphinstonian 1988-College Building Centenary Number (2).pdf
The Elphinstonian 1988-College Building Centenary Number (2).pdf
 
Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 

Recently uploaded

Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Ajax Part II

  • 1. AJAX – Part IIProgramming in Ajax Mukesh N. Tekwani tekwani@email.com
  • 2. Programming in Ajax We consider a program that asks the user to click a button, fetches data from the server using Ajax techniques, and displays that data in the same Web page as the button – without refreshing the page. November 25, 2010 2 Mukesh N Tekwani, Mumbai
  • 3. Programming in Ajax November 25, 2010 3 Mukesh N Tekwani, Mumbai
  • 4. Programming in Ajax November 25, 2010 4 Mukesh N Tekwani, Mumbai
  • 5. <body> <h1>Fetching data with Ajax</h1> <form> <input type = "button" value = "Display Message" onclick = "getData('http://localhost/AJAX/data.txt', 'targetDiv')"> </form> <div id = "targetDiv"> <p>The data fetched will go here</p> </div> </body> November 25, 2010 5 Mukesh N Tekwani, Mumbai
  • 6. The body of the page starts by displaying the original text in a <div> element <div id = "targetDiv"> <p>The data fetched will go here</p> </div> November 25, 2010 6 Mukesh N Tekwani, Mumbai
  • 7. There’s a button on this page. When the user clicks on the button, a JavaScript method named getData is called. <form> <input type = "button" value = "Display Message" onclick = "getData('http://localhost/AJAX/data.txt','targetDiv')"> </form> The getData function is passed two strings: November 25, 2010 7 Mukesh N Tekwani, Mumbai
  • 8. The first string is: onclick = "getData('http://localhost/AJAX/data.txt','targetDiv')"> The getData function is passed two strings: The name of the text file data.txt to fetch from the server, and The name of the <div> element to display the fetched text in. November 25, 2010 8 Mukesh N Tekwani, Mumbai
  • 9. Creating the XMLHttpRequest Object The code to create the XMLHttpRequest object is outside any function, so this code runs immediately as the page loads. We create a variable for this object XMLHttpRequestObject. var XMLHttpRequestObject = false; This variable is initialized to the value ‘false’ so that the script can check later whether the variable was created. November 25, 2010 9 Mukesh N Tekwani, Mumbai
  • 10. XMLHttpRequestObject The XMLHttpRequest object is part of the browser’s window object. So to check whether the XMLHttpRequest object is ready to use, we use the if statement. If XMLHttpRequest object is available, we can create the object as follows: if(window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } November 25, 2010 10 Mukesh N Tekwani, Mumbai
  • 11. XMLHttpRequestObject If we are working with Internet Explorer, we can create an XMLHttpRequest object like this: else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP"); } November 25, 2010 11 Mukesh N Tekwani, Mumbai
  • 12. XMLHttpRequestObject Now we have an XMLHttpRequest object. The properties and methods of the XMLHttpRequest object for IE are as follows: November 25, 2010 12 Mukesh N Tekwani, Mumbai
  • 13. XMLHttpRequestObject onreadystatechange – holds the name of the event handler that should be called when the value of the readystate property changes. In our example, it is function() readyState – holds the state of the request November 25, 2010 13 Mukesh N Tekwani, Mumbai
  • 14. Checking to make sure we have the XMLHttpRequestObject The getData() function will be used to actually fetch the text data from the file We first check that there is a valid object with the statement if(XMLHttpRequestObject) If the object creation hadn’t worked, then this variable will hold a value ‘false’ and the condition would become false. November 25, 2010 14 Mukesh N Tekwani, Mumbai
  • 15.
  • 16. This object has a method called “open()”. We will use that method to use the URL we want.
  • 17. Syntax of open() method is:
  • 18. open (“method”, “URL”, [,asyncFlag [, “username” [, “password”]]])November 25, 2010 15 Mukesh N Tekwani, Mumbai
  • 19.
  • 20. open (“method”, “URL”, [,asyncFlag [, “username” [, “password”]]])
  • 21. method – the HTTP method used to open the connection such as GET, POST, PUT, HEAD
  • 22. URL – the requested URL
  • 23. asyncFlag – A boolean value indicating whether the call is asynchronous or synchronous. Default is ‘true’ (asynchronous)November 25, 2010 16 Mukesh N Tekwani, Mumbai
  • 24.
  • 25. To open a URL we can use the GET, POST or PUT methods.
  • 26. But in Ajax, we will use GET to retrieve data and POST to send a lot of data to the serverXMLHttpRequestObject.open("GET", dataSource); November 25, 2010 17 Mukesh N Tekwani, Mumbai
  • 27.
  • 28. By default the connection to this file is made asynchronously. It means that this statement does not wait until the connection is made and the data is finished downloadingNovember 25, 2010 18 Mukesh N Tekwani, Mumbai
  • 29.
  • 30. If we assign the name of a JavaScript function to this property, that function will be called each time the XMLHttpRequest object’s status changes.November 25, 2010 19 Mukesh N Tekwani, Mumbai
  • 31.
  • 32. We have used a shortcut to assign a JavaScript function to the onreadystatechange property.
  • 33. We have created an anonymous function because the function does not have any name. Such a function is created on the fly, just by using the keyword function(). Then we define the body of this function in curly braces:November 25, 2010 20 Mukesh N Tekwani, Mumbai
  • 34.
  • 35.
  • 37. readyState property that tells us how the data downloading is progressing.
  • 38. The readyState property can have these values:
  • 44. We have used the value 4, which means the data is downloadedNovember 25, 2010 22 Mukesh N Tekwani, Mumbai
  • 45.
  • 47. This property that tells us the status of the download itself.
  • 48. It is the standard HTTP status code that the browser got for the URL you supplied.
  • 49. The possible values of status are:
  • 51. 400 – Bad request
  • 53. 404 – Not found
  • 54. 408 – request time out
  • 55. 414 – Requested URL too longNovember 25, 2010 23 Mukesh N Tekwani, Mumbai
  • 56.
  • 57.
  • 58. If we want to treat the data as standard text, we use the object’s responseText property.
  • 59. If the data is in XML format, we can use the responseXML property.
  • 60. To make the test appear on the page, we assign that text to the <div> element whose ID is targetDiv. This ID was passed to the getData() function.November 25, 2010 25 Mukesh N Tekwani, Mumbai
  • 61.
  • 62. We use the send() method.
  • 63.
  • 64.