May 29, 2017

Ajax Tutorial: XMLHttpRequest Overview

XMLHttpRequest was designed by MS to allow IE to load XML documents from JavaScript.

XMLHttpRequest provides a small API and a handful of methods and properties, XMLHttpRequest has difference between IE and Mozilla.In IE, XMLHttpRequest is an ActiveX object whereas,the Mozilla syntax for XMLHttpRequest instantiation is used.

XMLHttpRequest is the most important method for AJAX communications because of two unique features.

  1. Provides the ability to load new content without that content being changed in any way, which makes it extremely easy to fit AJAX into your normal development patterns.
  2. Allows JavaScript to make synchronous calls.

XMLHttpRequest Methods

XMLHttpRequest::Open() : The open method is used to set the request type (GET, POST, PUT, or PROPFIND),

For Example:

var req = new XMLHttpRequest();
req.open(‘GET’, ‘adress.html’, false);

XMLHttpRequest::Send() The send method makes the connection to the URL specified in open

For Example:

var req = new XMLHttpRequest();
req.open(‘POST’, ‘index.html’, false);
req.setRequestHeader(‘Content-type’,
‘application/x-www-form-urlencoded;charset=UTF-8;’);
req.send(‘hello=world&XMLHttpRequest=test’);

XMLHttpRequest::setRequestHeader() to set a header on a request

setRequestHeader(header,value)

XMLHttpRequest::getResponseHeader() and getAllResponseHeaders() :The geTResponseHeader method allows you to get a single header from the response ,getAllResponseHeaders returns all the headers from the response in a single string

getResponseHeader(header)