Asynchronous Data Transfers


If you are new to data transfers and you are not exactly sure what the term asynchronous data transfers actually means, don't worryyou are probably unaware of the fact that you already understand them and have used them while developing in other languages. This type of transaction is most typical of programming languages, and is a very important part of the XHR and, ultimately, all enterprise Ajax applications. This section will demystify asynchronous data transfers before we begin to dive into coding the object-oriented Ajax engine.

Asynchronous data transfers are a type of two-way communication that occurs with a time delay, allowing data to respond on its own time, when it is available. In other words, you can make a request to the server, continue to process other data, and receive a response at the server's leisure, thus making web applications very flexible. Asynchronous is the default nature of the request/response model of the XHR in an Ajax engine. This means that the request/response data is not transferring at predetermined or regular set intervals. For example, you can make an HTTP request to a server and continue to process other client-side interactions while waiting for the response in the background. This can all be happening while the user is working on other tasks or performing other interactionscompletely unaware of the data processing in the background. This means that you can make calls to a server-side language to retrieve data from a database, and return that data as XML, JSON, or text. You can also send data to a server-side language to be stored in a database, or you can simply load a static XML, JSON, or text file to dynamically populate pages of your website without refreshing the page or interrupting user interaction on the front end.

In order to process this request, we must first call two XHR methods: open and send. The open method of the XHR object takes three parameters. The first is a string that represents the method in which the request is to be sent. This method value can be GET, POST, or PUT. The second parameter is the URL that is being requested in the form of a string, which can be XML, JSON, a text file, or a server-side language that returns any of these formats. The last parameter, which happens to be the one that we are focusing on, is a Boolean that has a default value of true for asynchronous and false for synchronous. The send method follows open and is the actual method that sends the HTTP request and receives a response in the format that you specify. This method takes one string parameter, which can be XML or a simple key/value pair to be sent as a POST. Here is an example of the open and send methods as they would be used in a simple Ajax request:

request.open("method", "URL", true); request.send(null);


Asynchronous data transfers can prove to be complicated to manage in large-scale situations, but they are far more scalable and usable than synchronous data transfers. In order to serve complex audiences with varying skills and experience, developers need to create complex applications that can handle many tasks. Asynchronous interactions can supply this audience with the possibilities of multitasking and completing tasks efficiently, without the hassle of waiting for server responses. Synchronous transactions wait for a response to one request before another can be made. In a robust web application, this type of transaction could easily freeze up the page while the server is processing the requests in a queue, one after the other. Ultimately, this would be unusable and could easily turn users away.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net