send ajax Request (XMLHttpRequest) to a server

the xmlhttprequest object :- it is an api available in web browser scripting languages such as javascript. it is used to send http or https requests directly to a web server and load the server response data directly back into the script.

the data might be received from the server as json ,xml html or as plain text.

data from the response can be used directly to alter the dom of the currently active document in the browser window without loading a new web page document.

how send ajax request to a server

when we want to send a ajax request to a server, we use the open() and send() methods of the xmlhttprequest object .

the syntax :-

xmlhttp.send (content); (method,url, async, username, password);

ajax methoddescription

send the request.

content indicate name/value pairs, pass with post request.

open(method,url,async)method - type of request methods get,post or head.

url : indicate the location of file on the server.


true :- request generate as asynchronous type.

false : request generate as synchronous type.

much more ajax methods

ajax methoddescription
abort()cancels the current request.
getallresponseheaders()returns the complete set of http headers as a string.
getresponseheader(headername)returns the value of specified http header.
setrequestheader(label,value)set http headers to the request.

label :- indicate the header name

value :- indicate the header value.

request methods

the http/1.0 specification defined the get post and head methods.but over the internet only get/post method is usefull.the get method is simpler and faster than post method.

get requests :--

the http get request method is designed to retrieve information from the server.

if you wan to send some information with the get method, add the information to the url with (?) sign.

simple demo

post requests

post is non-idempotent type method, we can sending large amount of data to the server because post has no size limitations.the post is more robust and secure than get method.

example :

when a web browser sends a request from a web form element, the default internet media type is "application/x-www-form-urlencoded".this is a format for encoding key-value pairs with possibly duplicate keys.

example :how set the http header, specifies the data you want to send in the send() method.

the open() method description

the syntax : (method,url,asynchronous);

method - parameter is indicate the type of request, which can be get or post.

url : open() method url parameter is an address to a file on the server.the file can be any number of tyes such as .xml .text .asp .jsp .php .

asynchronous - ("get","signin.jsp",true);

when using asynchronous='true', assign a callback handler to the onreadystatechange property to determine when the call has completed.default is true.

example :- ("get","signin.jsp",false);

when using asynchronous='false' send operations are synchronous.when a send operation is in progress, other operations are suspended.

note:- if you use asynchronous='false', do not write an onreadystatechange function.

example :-