Thursday 31 March 2016

XMLHttpRequest


XMLHttpRequest:

  1. XMLHttpRequest
  2. Properties of XMLHttpRequest
  3. Methods of XMLHttpRequest

An object of XMLHttpRequest is used for asynchronous communication between client and server.

  • It performs following operations:
  • Sends data from the client in the background
  • Receives the data from the server
  • Updates the webpage without reloading it.
  • Properties of XMLHttpRequest object

  • The common properties of XMLHttpRequest object are as follows:
  • PropertyDescription
  • onReadyStateChange It is called whenever readystate attribute changes. It must not be used with synchronous requests.
  • readyState represents the state of the request. It ranges from 0 to 4.

  • 0 UNOPENED open() is not called.
  • 1 OPENED open is called but send() is not called.
  • 2 HEADERS_RECEIVED send() is called, and headers and status are available.
  • 3 LOADING Downloading data; responseText holds the data.
  • 4DONE The operation is completed fully.
  • reponseText returns response as text.
  • responseXML returns response as XML


Methods of XMLHttpRequest object

  1. The important methods of XMLHttpRequest object are as follows:
  2. MethodDescription
  3. void open(method, URL) opens the request specifying get or post method and url.
  4. void open(method, URL, async) same as above but specifies asynchronous or not.
  5. void open(method, URL, async, username, password) same as above but specifies username and password.
  6. void send() sends get request.
  7. void send(string) send post request.
  8. setRequestHeader(header,value) it adds request headers.