views:

173

answers:

1

I've a AJAX call, that pulls some initial data. Based on this data, I want to fire a series of AJAX calls, updating different parts of the page. It can be done using JS, with the XMLHttpRequest and onreadystatechange, checking for the status. If the first call is done and the response is 200, I can just fire a series of AJAX calls.

How to do the same thing in Rails? Or should I just write plain JS (I'm new to Rails)?

A: 

You have to consider both server-side (rails or whatever) and client-side (javascript).

For client-side:
First, I recommend you to use a javascript framework such as jQuery, Mootools, Prototype, Dojo, or whatever. It will hide difference among web browsers, so you can focus on your logic. (XMLHttpRequest has different initialization process on various web browsers)

Then you may make use of oncomplete event or something similar to that to chain subsequent AJAX calls. Every javascript framework provides good AJAX library functions though they have different styles.

For server-side:
If each subsequent call depends on the previous call, you should validate inputs from each AJAX call to ensure correctness of your program. (This means, there might exist malicious clients that send subsequent AJAX requests even when the first call has failed.) The validation must be done at server-side.

To maintain consistent context with a specific user (or webbrowser), you may have to use session.

Achimnol