views:

423

answers:

8

When is it appropriate to use AJAX?
what are the pros and cons of using AJAX?

In response to my last question: some people seemed very adamant that I should only use AJAX if the situation was appropriate: Should I add AJAX logic to my PHP classes/scripts?

In response to Chad Birch's answer: Yes, I'm referring to when developing a "standard" site that would employ AJAX for its benefits, and wouldn't be crippled by its application. Using AJAX in a way that would kill search rankings would not be acceptable. So if "keeping the site intact" requires more work, than that would be a "con".

A: 

Ajax is primarily used when you want to reload part of a page without reposting all the information to the server.

Cons:

  • More complicated than doing a normal post (working with different browsers, writing server side code to hadle partial postbacks)
  • Introduces potential security vulnerabilities ( You are introducing additional code that interacts with the server. This can be a problem on both the client and server.
    On the client, you need ways of sending and receiving responses. It's another way of interacting with the browser which means there is another point of entry that has to be guarded. Executing arbritary code, posting data to a non-intended source etc. There are several exploits for Ajax apps that have been plugged over time, but there will always be more. )

Pros:

  • It looks flashier to end users
  • Allows a lot of information to be displayed on the page without having to load all at the same time
  • Page is more interactive.
Kevin
Explain the security vulnerabilities that are created by using Ajax.
Allain Lalonde
Shouldn't be any - the server is still responsible for enforcing it.
Don Branson
You can't be serious? You don't think there are any additional security vulnerabilities when implementing ajax?
Kevin
Yes, I'm serious. All of the issues with security that exist with ajax already exist with conventional http requests. Ajax doesn't add anything to the ways that we interact with the browser or the server. It's using techniques that already existed on both ends
Don Branson
It's not the base technolgy that's the hole. It's the mentality as to how the technology is used that causes the problem. Ajax changed how web applications are created and with that new exploits were exposed. Certain assumptions about what made an application secure no longer apply.
Kevin
+4  A: 

When you are only updating part of a page or perhaps performing an action that doesn't update the page at all AJAX can be a very good tool. It's much more lightweight than an entire page refresh for something like this. Conversely, if your entire page reloads or you change to a different view, you really should just link (or post) to the new page rather than download it via AJAX and replace the entire contents.

One downside to using AJAX is that it requires javascript to be working OR you to construct your view in such a way that the UI still works without it. This is more complicated than doing it just via normal links/posts.

tvanfosson
+2  A: 

AJAX is usually used to perform an HTTP request while the page is already loaded (without loading another page).

The most common use is to update part of the view. Note that this does not include refreshing the whole view since you could just navigate to a new page.

Another common use is to submit forms. In all cases, but especially for forms, it is important to have good ways of handling browsers that do not have javascript or where it is disabled.

grammar31
+7  A: 

It's a pretty large subject, but you should be using AJAX to enhance the user experience, without making the site totally dependent on it. Remember that search engines and some other visitors won't be able to execute the AJAX, so if you rely on it to load your content, that will not work in your favor.

For example, you might think that it would be nice to have users visit your blog, and then have the page dynamically load the newest article(s) with AJAX once they're already there. However, when Google tries to index your blog, it's just going to get the blank site.

A good search term to find resources related to this subject is "progressive enhancement". There's plenty of good stuff out there, spend some time following the links around. Here's one to start you off:

http://www.alistapart.com/articles/progressiveenhancementwithjavascript/

Chad Birch
Thank you for your first paragraph. In reference to the blog portion, however, couldn't you just take a normal blog page, that would be indexed, and add AJAX after the fact?
SkippyFire
Yes, that's exactly what "progressive enhancement" means. The page should still be totally functional for someone (or somebot) that can't use AJAX, but it should get -better- if they can. Plenty of sites make this mistake, try using NoScript with scripts off by default, many sites don't work at all.
Chad Birch
A good code design principle is: you never want to break the experience for people who don't or can't use Javascript.
bigmattyh
+1  A: 

Javascript should always just be an addition to the functionality of your website. You should be able to use and navigate the site without any Javascript involved. You can use Javascript as an addition to existing functionality, for example to avoid full-page reloads. This is an important factor for accessibility. Javascript should never be used as the only possibility to reach or complete a request on your site.

As AJAX makes use of Javascript, the same applies here.

Raim
+2  A: 

I think the advantage of using ajax technologies isn't only for creating better user-experiences, the ability to make server calls for only specific data is a huge performance benefit.

Imagine having a huge bandwidth eater site (like stackoverflow), most of the navigation done by users is done through page reloads, and data that is continuously sent over HTTP.

Of course caching and other techniques help this bandwidth over-head problem, but personally I think that sending huge chunks of HTML everytime is really a waste.

Cons are SEO (which doesn't work with highly based ajax sites) and people that have JavaScript disabled.

Luca Matteis
I would add that AJAX is a performance benefit when used *judiciously*. Too much of it, and you're causing a performance problem on the user's end *and* on the server end.
Jason Baker
A: 

When your application (or your users) demand a richer user experience than a traditional webpage is able to provide.

Wayne M
+1  A: 

Ajax gives you two big things:

Responsiveness - you can update only parts of a web page at a time if need be (saving the time to re-load a page). It also makes it easier to page data that is presented in a table for instance.

User Experience - This goes along with responsiveness. With AJAX you can add animations, cooler popups and special effects to give your web pages a newer, cleaner and cooler look and feel. If no one thinks this is important then look to the iPhone. User Experience draws people into an application and make them want to use it, one of the key steps in ensuring an application's success.

For a good case study, look at this site. AJAX effects like animating your new Answer when posted, popups to tell you you can't do certain things and hints that new answers have been posted since you started your own answer are all part of drawing people into this site and making it successful.

Jeffrey Cameron