aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes How JavaScript talk with Server ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How JavaScript talk with Server ?" Watch "How JavaScript talk with Server ?" New topic
Author

How JavaScript talk with Server ?

Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Hi Ranchers,

Ajax is a web development technique for creating interactive web applications.
only that the thing which i know, javaScript and XML langauges are responsible to exchanging small amounts of data with the server behind the scenes.

Can anyone clear me on this AJAX process how its work with javaScript because I am confused how can javaScript talk with Server as I read in books javaScript is client side scripting.

Thanks,


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ajax uses the XMLHttpRequest object to exchange data asynchronously.
You set an event handler(javascript) to be called whenever the state of the server changes, so you can grab the data sent by the server when he has finished.

Note that you are not limited to XML to send data. Any format (plain text, JSON...) will do.

For more about XMLHttpRequest:
http://en.wikipedia.org/wiki/XMLHttpRequest
[ October 03, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

thanks for the reply Satou kurinosuke,

but my question is still unanswered how java Script talk with Server ?
i mean how javaScript passing and recieveing a data from Server.
or send me any link with defined process of AJAX.. I like to read and understand and also want to implements in My Module Assignment.
[ October 03, 2006: Message edited by: Saif Uddin ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I think it answers it all
Javascript uses XMLHttpRequest to talk with the server. You get an instance of XMLHttpRequest, you set a few information (like GET or POST, the url, request params...) and you call send().
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

thanks Satou,

so this is about the request to fetch data from server but what about the response where I get Response from the Server.

Can you provide me any link, article, examples or any resource. where i get the whole idea about AJAX.

thanks in advance..
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

About the response.
The server outputs data in the response. So how do you get it from Javascript ? Simple. Before making a request, you declare an event handler to be called when the state of the server changes. So this handler will also be triggered when has finished.

You still use the XMLHttpRequest for this. You set your event handler to a property called onreadystatechange.

Try a tutorial, you'll get the whole picture (try Google).
I can recommend you a book called Head Rush Ajax to understand all the basics in no time.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


This is the code snippet of a JS function which is going to make asynchronous request.

Here, first line is initializing the connection, 'GET' is type of request (get or post etc), second parameter is the URL where browser should send the request, and third parameter is specifying it is asynchronous request (false for synchronous request).

In the second line, we are saying to browser that run updatePage() JS function whenever ready state changes.

In the third line, we make browser sending the request with no parameters.

This is how it works.

Head Rush is great book as usual.
[ October 03, 2006: Message edited by: rathi ji ]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Thanks for the reply of both of you..

but I am still searching the tutorial of Ajax.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

http://www.w3schools.com/ajax/default.asp
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Thanks Satou,

that's the thing which is left in this thread what I am waiting for..

Thanks again..
 
wood burning stoves
 
subject: How JavaScript talk with Server ?