When is it appropriate to use AJAX?

2019-05-10 15:06发布

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

8条回答
小情绪 Triste *
2楼-- · 2019-05-10 15:42

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

查看更多
该账号已被封号
3楼-- · 2019-05-10 15:43

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/

查看更多
你好瞎i
4楼-- · 2019-05-10 15:46

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.
查看更多
一纸荒年 Trace。
5楼-- · 2019-05-10 15:51

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.

查看更多
姐就是有狂的资本
6楼-- · 2019-05-10 15:59

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.

查看更多
戒情不戒烟
7楼-- · 2019-05-10 16:04

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.

查看更多
登录 后发表回答