Jump to content

AJAX - The Easy Way


ohdang888

Recommended Posts

When I see people wanting help in AJAX, they often have errors in setting up the XMLHttpRequest object, or dealing with the response, etc. So I decided to this post for quick reference for people to find out the easy way to do AJAX.

 

 

The OpenSource project is called Prototype. Download the file here:

http://www.prototypejs.org/download

The api doc for what i'll be writing about can be found here:

http://www.prototypejs.org/api/ajax/request

 

I had a little trouble with it at first, but once you realize its potential you'll love it.

 

Include the File in your document

<script type="text/javascript" src="prototype.js"></script>

 

 

Now, let's do the AJAX request itself.

<script type="text/javascript">
var url = "path/to/handler.php";
var gender = "male";
var how_old = "really old";

new Ajax.Request(url, {
  method: 'get',
  parameters: {'gen': gender, 'age': how_old},
  onSuccess: success,
  onFailure: failure, 
  });


function failure(){
alert('FAILURE!!!!');
}

function success(){
alert('Wo! Success!');
}

</script>

Lets go over the parts of the request:

1)url - the url to the file that will handle the request, relative to your current directory

2)method - get or post, i suggest get

3)parameters - You don't have to assemble the vars for the url. (none of "?gen=male&age=really_old)..just declare 'name' : 'value'...and for variables, don't put quote around it the variable itself...you would use 'name' : variable

 

4)onSuccess & onFailure - After the request is made, it will do one of these two things depending on if the call is successful, or failure(error recieved).

 

For onSuccess and onFailure, you will list a function it should carry out. Do NOT list the "()" after the function name. Just list the name of the function...for example..i would not write failure(), i would simply write the word failure

 

There are many more options like this you can add. These are the basic and most important.

You can view the other options here(Under CallBacks):

http://www.prototypejs.org/api/ajax/options

 

 

 

Also, its fine to not declare onSuccess or onFailure

For instance, this is perfectly acceptable:

new Ajax.Request(url, {
  method: 'get',
  parameters: {'gen': gender, 'age': how_old}
  });

 

Handling the Response

If you have to handle response text from the file handling the request, you must declare the function within the request.

 

Also, you use transport.responseText as the variable that contains the text.

 

example:

new Ajax.Request(url, {
  method: 'get',
  parameters: {'gen': gender, 'age': how_old}
  onSuccess: function(transport) {
  alert(transport.responseText);
    },
  onFailure: failure
  });

That will pop an alert box with the response text

 

There's a whole number of objects within the transport, view them here:

http://www.prototypejs.org/api/ajax/response

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.