在什么情况下不应该使用AJAX? [英] What are the cases when AJAX should not be used?

查看:66
本文介绍了在什么情况下不应该使用AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AJAX的站点和应用程序似乎正在迅速增长.使用AJAX的主要原因之一可能是为了增强用户体验.我担心的是,仅仅因为该项目可以使用AJAX,并不意味着它应该.

It seems that the sites and applications that use AJAX are growing rapidly. And probably one of the major reasons for using AJAX is to enhance the user experience. My concern is that just because the project CAN use AJAX, doesn't mean that it SHOULD.

可能是AJAX出于UX的目的向站点/应用程序暴露了更多的安全威胁.也许您还有其他原因不使用AJAX.

It could be that AJAX is exposing more security threats to the site/app for the sake of UX. Maybe you have other reasons for not using AJAX.

何时应避免使用AJAX?

When should the use of AJAX be avoided?

推荐答案

获取数据已可用或易于获取数据

我通常会在汽车网站上看到这一点,那里有品牌和型号.他们通常的< select> (不带JavaScript)包括这样的< optgroup> :

Getting Data Already Available Or Data Easily Obtainable

I usually see that on car websites, where there are makes and models. Their usual <select> (without JavaScript) include <optgroup>'s as such:

<select>
    <optgroup label="Ford">
        <option value="21">Escape</option>
        <option value="21">F-150</option>
    </optgroup>
    <optgroup label="Toyota">
        <option value="51">Corolla</option>
        <option value="52">Yaris</option>
    </optgroup>
</select>

然后通常会继续隐藏该< select> 并创建2个新选择,一个用于品牌,一个用于模型.

Then usually then proceed to hide that <select> and create 2 new select, one for makes and one for models.

到目前为止,一切都很好.他们开始在这里搞砸了.

All is fine up to that point. They start messing up here.

然后,当他们可以简单地解析原始元素以尝试获取其信息时,他们便处理查询服务器以获取制造商列表,然后再次进行查询以获取模型列表.然后,每次更改品牌时,都会发出另一个请求...

They then process to query the server to get a list of makes, and then do another query to get the list of models, when they could of simply parsed the original element try to get their information. Then, everytime you change make, another request is made...

以上是何时不使用AJAX的一个很好的例子.考虑一下:一个请求比解析可用数据长,因此它们使用户等待.他们可能每次都查询数据库,因此数据库的服务器CPU使用率受到影响.并且它招致更多的带宽.可怕的资源浪费.

The above is a excellent example of when NOT to use AJAX. Consider this: a request is longer than parsing available data, so they make the user wait. They probably query their database each time, so it has a hit on their server CPU usage. And it incurs more bandwidth. Terrible waste of resources.

他们应该已经简单地解析了< select> 下的DOM try来获取相关信息.每个< optgroup> 是makes < select> 中的一项,而每个< option> 中的每个子代< option> optgroup> 是模型< select> 中的一项.

They should of have simply parsed the DOM try under the <select> to fetch the relevant information. Each <optgroup> is an item in the makes <select> while each child <option> from the <optgroup> is an item in the models <select>.

  • 使用AJAX进行简单的静态DOM修改(在大多数情况下,不需要AJAX从一个选项卡切换到另一个选项卡,只需将数据包含在原始请求中即可).
  • 使用AJAX在加载时检索数据(为什么不将其包含在原始请求中?)
  • 使用AJAX作为图片库(为什么不包含图片并在请求完成后对其进行处理?)

这篇关于在什么情况下不应该使用AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆