XMLHttp Object.

Slides:



Advertisements
Similar presentations
9. AJAX & RIA. 2 Motto: O! call back yesterday, bid time return. — William Shakespeare.
Advertisements

Asynchronous HTTP request generation in JavaScript (AJAX)
AJAX asynchronous server-client communication. Test.
XMLHttpRequest Object and XML What we should learn in this lesson –What is the XHR object? –How to create the XHR objects? –XHR object properties –XHR.
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
Interactive Web Application with AJAX
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
Ajax Basics The XMLHttpRequest Object. Ajax is…. Ajax is not…. Ajax is not a programming language. Ajax is not a programming language. Ajax is a methodology.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
JavaScript & jQuery the missing manual Chapter 11
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
Posting XML Data From the Client to a Server Eugenia Fernandez IUPUI.
Intro to Ajax Fred Stluka Jan 25, /25/2006Intro to AjaxFred Stluka2 What is Ajax? "Asynchronous JavaScript and XML" New name for an old technique:
1 rfXcel Confidential Copyright 2007 Web Technology JavaScript 12/10/07.
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
XMLHttpRequest Object When Microsoft Internet Explorer 5.0 introduced a rudimentary level of XML support, an ActiveX library called MSXML was also introduced,
Random Logic l Forum.NET l AJAX Behind the buzz word Forum.NET ● January 23, 2006.
Ajax In Action The Journey into Web2.0 Presented by Eric Pascarello.
AJAX (also known as: XMLHTTP, Remote Scripting, XMLHttpRequest, etc.) Matt Warden.
Lecture 9: AJAX, Javascript review..  AJAX  Synchronous vs. asynchronous browsing.  Refreshing only “part of a page” from a URL.  Frameworks: Prototype,
AJAX محمد احمدی نیا 2 Of 27 What is AJAX?  AJAX = Asynchronous JavaScript and XML.  AJAX is not a new programming language, but.
AJAX Compiled from “AJAX Programming” [Sang Shin] (Asynchronous JavaScript and XML)
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 2 – Ajax Fundamentals.
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
Ajax ( Asynchronous JavaScript + XML ) Information Science and Knowledge Systems: Semantic Web Spring 2006 / Dr. Don Turnbull Presented by David Kim.
School of Computing and Information Systems CS 371 Web Application Programming AJAX.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
AJAX Asynchronous JavaScript & XML A short introduction.
SYST Web Technologies SYST Web Technologies AJAX.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
AJAX AJAX Asynchronous JavaScript and XML --- MADHAVI
What is AJAX ? Asynchronous Javascript and XML. Not a stand-alone language or technology. It is a technique that combines a set of known technologies in.
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
Internet Technologies #6 REST SOAP AJAX. Agenda  REST  SOAP  AJAX.
Web Technology (NCS-504) Prepared By Mr. Abhishek Kesharwani Assistant Professor,UCER Naini,Allahabad.
 AJAX technology  Rich User Experience  Characteristics  Real live examples  JavaScript and AJAX  Web application workflow model – synchronous vs.
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
JavaScript, Sixth Edition Chapter 11 Updating Web Pages with Ajax.
JavaScript and Ajax Week 10 Web site:
CITA 330 Section 10 Web Remoting Techniques. Web Remoting Web Remoting is a term used to categorize the technique of using JavaScript to directly make.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
Ajax SUBMITTED BY NITIN RAMANI C.S.E 3 rd Y 5 th S R.N CS SUBMITTED TO PRO. PUSHPARAJ PATEL SIR.
AJAX Rohan B Thimmappa. What Is AJAX? AJAX stands for Asynchronous JavaScript and XML. AJAX stands for Asynchronous JavaScript and XML. A remote scripting.
Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.
JavaScript and Ajax (Ajax Tutorial)
CSE 154 Lecture 11: AJAx.
Understanding XMLHttpRequest
Not a Language but a series of techniques
AJAX AJAX = Asynchronous JavaScript and XML.
CS 371 Web Application Programming
AJAX.
CSE 154 Lecture 11: AJAx.
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
JavaScript & AJAX.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Chengyu Sun California State University, Los Angeles
AJAX CS-422 Dick Steflik.
Intro to Ajax Fred Stluka Jan 25, 2006.
Web Technology Even Sem 2015
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
AJAX Chapters 19, 20.
Presentation transcript:

XMLHttp Object

Initially created by Microsoft as a part of its ActiveX library called MSXML to provide support for XML in their IE 5.0

It was created to initiate HTTP requests from anywhere in an application.

These requests were intended to return XML.

It could be used in both web pages and desktop applications.

Now Mozilla, Safari and Opera also provide their version of the object.

Var oxmlhttp=new ActiveXObject(“Microsoft.XMLHttp); Creating XMLHttp Object: Var oxmlhttp=new ActiveXObject(“Microsoft.XMLHttp);

Different versions of XMLHttp Object: Microsoft.XMLHttp MSXML2.XMLHttp MSXML2.XMLHttp.3.0 MSXML2.XMLHttp.4.0 MSXML2.XMLHttp.5.0

Java Script to Check for XMLHttp Object Version: Function createHTML() { Var versions=[“MSXML.XMLHttp.5.0”, “MSXML2.XMLHttp.4.0”, “MSXML2.XMLHttp.3.0”, “MSXML2.XMLHttp”, “Microsoft.XMLHttp” };

For( var I = 0; I < versions.length; i++) { try{ Var oxmlhttp=new ActiveXObject(versions[i]); Return oxmlhttp; }

For( var I = 0; I < versions.length; i++) { try{ Var oxmlhttp=new ActiveXObject(versions[i]); Return oxmlhttp; }

catch (oError) { //Do nothing } throw new Error("MSXML is not installed.");

In Other Browsers: Var oxmlhttp=new XMLHttpRequest()

Creating XMLHttp Object depending on the browser:

Using XMLHttp Object: Open() : used to initialize the object. Open(rquesttype, URL, Async) Requesttype: GET/POST URL : a string indicating URL to send request to. Async : Synchrnous/Asynchronous (true/false)

To make asynchronous requests var oxmlhttp=XmlHttp.createRequest(); oxmlhttp.open(“get”, “phone.txt”, true)

XMLHttp.readystate property: Readystate property changes value as the request goes through and the response is received.

There are five possible values for readyState: 0 (Uninitialized): The object has been created but the open() method hasn't been called. 1 (Loading): The open() method has been called but the request hasn't been sent. 2 (Loaded): The request has been sent. 3 (Interactive). A partial response has been received. 4 (Complete): All data has been received and the connection has been closed.

Because of differences in browser implementations, the only reliable readyState values for cross-browser development are 0, 1, and 4. In most cases, however, you will check only for 4 to see when the request has returned:

So you need to define an onreadystatechange event handler to handle readystatechange event.

Every time the readyState property changes from one value to another, the readystatechange event fires and the onreadystatechange event handler is called.

var oXmlHttp = zXmlHttp. createRequest(); oXmlHttp. open("get", "info var oXmlHttp = zXmlHttp.createRequest(); oXmlHttp.open("get", "info.txt", true); oXmlHttp.onreadystatechange = function () { if (oXmlHttp.readyState == 4) alert("Got response."); } }; oXmlHttp.send(null);

send() method: will actually send the request. It takes String type of argument If the request does not require a body, then null is passes.

To retrieve data returned you can use the following properties: responseText: returns string containing the response body. responseXML: returns XML data got in response / if the data has text/XML as content-type.

So, to get the text contained in phone So, to get the text contained in phone.txt, the call would be as follows: var sData = oXmlHttp.responseText;

status property: contains the HTTP status code sent in the response. statusText: contains the text description of the status (such as "OK" or "Not Found").

200 code means request was completed succesfully. if (oXmlHttp.status == 200) { alert("Data returned is: "+ oXmlHttp.responseText; } else alert("An error occurred: "+ oXmlHttp.statusText; 200 code means request was completed succesfully.

The readyState property is set to 4 even if a server error occurred, so just checking that is not enough. In the above example, the responseText property is shown only if the status is 200; otherwise, the error message is displayed.

getResponseHeader() method: is used to access response headers

var sContentType = oXmlHttp.getResponseHeader("Content-Type"); if (sContentType == "text/xml") { alert("XML content received."); } else if (sContentType == "text/plain") alert("Plain text content received."); alert("Unexpected content received.");

setRequestHeader() method: is to specify the headers info you are sending out with the request.

var oXmlHttp = zXmlHttp. createRequest(); oXmlHttp. open("get", "info var oXmlHttp = zXmlHttp.createRequest(); oXmlHttp.open("get", "info.txt", true); oXmlHttp.onreadystatechange = function () { if (oXmlHttp.readyState == 4) alert("Got response."); } }; oXmlHttp.setRequestHeader("myheader", "myvalue"); oXmlHttp.send(null);

synchronous requests : var oXmlHttp = zXmlHttp.createRequest(); oXmlHttp.open("get", "info.txt", false); oXmlHttp.send(null); if (oXmlHttp.status == 200) { alert("Data returned is: "+ oXmlHttp.responseText; } else alert("An error occurred: "+ oXmlHttp.statusText;

XMLHttp using GET: XMLHttp using POST:

Advantages and Disadvantages of XMLHttp The user can view the output from the server in the same page. Http requests can be sent from anywhere in the page.

Client can have synchronous as well as asynchronous communication with the server. The code you write is much cleaner and the intent of the code is much more apparent than using numerous callback functions with hidden frames. You have access to request and response headers as well as HTTP status codes, enabling you to determine if your request was successful.

Disadvantages Disadvantage is that, unlike hidden frames, there is no browser history record of the calls that were made. The Back and Forward buttons do not tie in to XMLHttp requests, so you have effectively cut off their use.

It is for this reason that many Ajax applications use a mixture of XMLHttp and hidden frames to make a truly usable interface. Another disadvantage, which applies to Internet Explorer only, is that you depend on ActiveX controls being enabled.

If the user has your page set up in a particular security zone that doesn't allow ActiveX controls, you cannot access the XMLHttp object. In that case, you may have to default to using hidden frames.