XMLHttpRequest Object in Ajax

XMLHttpRequest (XHR) is an API available to web browser scripting languages such as JavaScript. It is used to send HTTP or HTTPS requests to a web server and load the server response data back into the script. Development versions of all major browsers support URI schemes beyond http: and https:, in particular, blob: URLs are supported.

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:

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.
4 DONE The operation is completed fully.

reponseText-
returns response as text.

responseXML-
returns response as XML

Methods of XMLHttpRequest object-

The important methods of XMLHttpRequest object are as follows:

void open(method, URL):
opens the request specifying get or post method and url.

void open(method, URL, async):
same as above but specifies asynchronous or not.

void open(method, URL, async, username, password):
same as above but specifies username and password.

void send():
sends get request.

void send(string):
send post request.

setRequestHeader(header,value):
it adds request headers.


Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports protocols other than HTTP (including file and ftp).

Create an XMLHttpRequest Object-

To create an instance of XMLHttpRequest, simply do this:

var myRequest = new XMLHttpRequest();

Old versions of Internet Explorer (IE5 and IE6) use an ActiveX Object:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");



<<Previous <<   || Index ||   >>Next >>








32 comments:

  1. good explanation dinesh!! will suggest it to my friends..

    ReplyDelete
  2. Thanks Saquib for response.

    Dinesh

    ReplyDelete
  3. MallikarjunReddy KamireddyJuly 2, 2014 at 9:56 PM

    Hi dinesh,
    i comeback to your site for learning EJB3.0 but It is not available in your site.Please post Spring MVC app with EJB .

    ReplyDelete
  4. MallikarjunReddy KamireddyJuly 2, 2014 at 9:57 PM

    we learn a lot from your site we really says thanks to you.

    ReplyDelete
  5. thanks very good sample source code..

    ReplyDelete
  6. If you are using Dao classes it is strongly coupled to the underlying data access framework. In this example, EmployeeDaoImpl extends JdbcDaoSupport and hence it is coupled to JDBC. If this is the case how can we "switch between the aforementioned persistence technologies fairly easily" ?

    ReplyDelete
  7. Please change context root of your application.

    ReplyDelete
  8. In spring.xml, NamedParameterJdbcTemplate should be given a constructor-arg instead of property.

    ReplyDelete
  9. very gooooddddddddddddddddd tutorials

    ReplyDelete
  10. i want to make a student management application using spring 3.0 ,how do i start

    ReplyDelete
  11. Have you hands on Spring 3 or you are new for it.

    ReplyDelete
  12. Please explain in details

    ReplyDelete
  13. Hi Dinesh plz share the url of RestClient Application. You are taking dynamicWebProject or javaProject. I am trying with javaProject but its giving bellow error. plz help me

    Caused by: java.net.ConnectException: Connection refused: connect

    ReplyDelete
  14. please make sure when you ran application your net connection working or not?

    ReplyDelete
  15. ya net connection is good dinesh. first let me conform one thing it is plain javaProject right?

    ReplyDelete
  16. plz share me any prerequirements is needed

    ReplyDelete
  17. Yes Ramesh it is plain java project but in this project we access one web service at plain java project so because of this need net connection.

    Are host this web service or web app?

    http://localhost:8181/sdnext/doj/webservice/json/employee/

    ReplyDelete
  18. you have download this application at this link
    http://www.dineshonjava.com/2013/06/restful-web-services-with-jersey-jax-rs.html#.VAgW11fm6Ph

    and deploy to your machine after that you can invoke through that java project application.

    ReplyDelete
  19. ok thank you for replaying Dinesh. your tutorial is very useful. I learn so many things in this tutorial... Great JOB...

    ReplyDelete
  20. i am new for spring 3

    ReplyDelete
  21. i want to make a student management application using spring 3.0 ,how do i start ,i am new to spring 3

    ReplyDelete
  22. i want to make a student management application using spring 3.0 ,how do i start ,i am new to spring 3

    ReplyDelete
  23. please replay me as early as possible

    ReplyDelete
  24. Hi Rahul,

    please start your project whenever you want any help you can contact me.

    ReplyDelete
  25. Thanks, very nice post

    ReplyDelete
  26. if next is failed in one step and still i want to go to next step how to handle that scenario

    ReplyDelete