2

我有一个 AJAX 调用,它会提取一些初始数据。基于这些数据,我想触发一系列 AJAX 调用,更新页面的不同部分。它可以使用 JS 来完成,使用 XMLHttpRequest 和 onreadystatechange,检查状态。如果第一次调用完成并且响应为 200,我可以触发一系列 AJAX 调用。

如何在 Rails 中做同样的事情?还是我应该只写普通的 JS(我是 Rails 新手)?

4

1 回答 1

0

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.

于 2009-06-13T14:18:47.230 回答