Creating HTTP Resources with Ajax

an article added by: Sonja Lande at 06012007


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

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

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 disclaimer

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.

related articles

1. 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...

2. 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...

3. 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 ...

4. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...

5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...

8. Implementing an Error and Exception Handling Strategy
Implementing an Error and Exception Handling Strategy Problem You want to implement a clean error and exception handling strategy in your applications, to make them run more smoothly. Theory Of course, you might argue that one error is a dialog box and the other is generated in the JavaScript console. The fact that one browser uses a dialog box to show an error and the other does not is a browser issue, not an error issue. A concise way of classifying the two errors is to ...

9. Understanding the Behavior of Variables When Implementing Recursion
Understanding the Behavior of Variables When Implementing Recursion Problem You want to implement recursion in JavaScript, and you also want to understand how variables will behave under those circumstances. Theory In JavaScript, you do not need to declare the variable type, or even declare the variable. For example, the following code works perfectly: if( counter == 1) { buffer = "counter is 1"; } document.getEle...