Creating HTTP Resources with Ajax

an article added by: Sonja Lande at 06012007


In: Categories » Computers and technology » AJAX » Creating HTTP Resources with Ajax

It would seem that the original idea of retrieving the unique URL in the body of the action URL response is the only solution, but never fear. Another solution does exist, and it’s compliant with the HTTP protocol. The HTTP status code 201 corresponds to a response where the server has created another URL that can be found in the Location HTTP header. This logic corresponds closely to what the role of the action URL is with respect to the unique URL. When the server responds with an HTTP status 201, the browser or XMLHttpRequest instance doesn’t perform a redirect automatically. It’s up to the code or browser to take action on the response. You can use the following JavaScript class to retrieve the unique URL from the action URL.

   Source: /client/scripts/jaxson/communications.js
   function UniqueURL( url) {
   this.asynchronous = null;
   this.baseURL = url;
   this.uniqueURL = null;
   this.haveIt = function() { }
   }
   UniqueURL.prototype.getIt = function() {
   var instance = this;
   this.asynchronous =  FactoryHttp.getAsynchronous();
   this.asynchronous.settings = {
   onComplete : function(xmlhttp) {
   if( xmlhttp.status == 201) {
   instance.uniqueURL = xmlhttp.getResponseHeader(  "Location");
   instance.haveIt();
   }
   }
   }
   this.asynchronous.get(this.baseURL);
   }
   UniqueURL.prototype.postIt = function( inpData)  {
   var instance = this;
   this.asynchronous =  FactoryHttp.getAsynchronous();
   this.asynchronous.settings = {
   onComplete : function(xmlhttp) {
   if( xmlhttp.status == 201) {
   instance.uniqueURL = xmlhttp.getResponseHeader(  "Location");
   instance.haveIt();
   }
   }
   }
   this.asynchronous.post(this.baseURL, inpData);
 }

The class UniqueURL is a single-purpose class, only in that you use it to retrieve the unique URL based on what is defined as the action URL. UniqueURL is instantiated, and the constructor requires you to define the action URL that is then assigned to the data member baseURL. You could call two methods to retrieve the unique URL: getIt and postIt. Both methods serve the same purpose, except getIt uses the HTTP GET verb, and postIt uses the HTTP POST verb. Internally, the Asynchronous methods are used to make the GET or POST requests. The methods would execute the action URL using either get or post methods and then await a response in the onComplete method. Because UniqueURL is only interested in a redirection, the onComplete method only processes HTTP status code 201. Every other status code is ignored. When the 201 status code is received, the method getResponseHeader retrieves the HTTP header location, as it contains the unique URL. Once the unique URL has been retrieved, the user-implemented method haveIt is called, indicating that a unique URL has been generated.

The implementation of UniqueURL is simple and only does one thing, which is convert an action URL into a unique URL. How the server generates the unique URL depends on how the consumer of UniqueURL called the server. In the case of the shopping cart example, all that is required is calling the method getIt. In the case of the bank account, the postIt method with appropriate data is called.

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. Ajax
Given the recent interest in Ajax, you’d be forgiven for thinking it was a new technology. In fact, the XMLHttpRequest object has been around for years. In technical terms, asynchronous JavaScript interaction with the server is nothing new. All of the other elements of the Ajax model have also been around for quite some time: CSS, (X)HTML, and DOM Scripting. Yet in 2005, interest in this methodology soared. Could it really be that simply giving this approach a snappy name like Ajax was responsible for the sudde...

2. Understanding the Definition and Philosophy of Ajax
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...

3. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...

4. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

5. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

6. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

7. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...