如何AJAX的工作? [英] How does AJAX work?

查看:175
本文介绍了如何AJAX的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是AJAX的本质是什么?例如,我想在我的网页的链接,这样当用户点击这个链接,信息会被发送到我的服务器没有当前页面的重新加载。那是AJAX?

What is the essence of AJAX? For example, I want to have a link on my page such that when a user clicks this link, some information is sent to my server without the reloading of the current page. Is that AJAX?

我能够用isoframes来获得这一行为。更细节我把一个链接(比方说一个小图像)在一个小isoframe。当用户点击这个链接,浏览器只是重新加载页面的isoframe。

I was able to get this behavior by using isoframes. In more details I put a link (let's say a small image) in a small isoframe. When the user clicks this link the browser reloads only the page in the isoframe.

不过,我认为它不是一种优雅的方式来达到目标​​。我想我必须使用AJAX。它是如何工作的? XHTML的使用可以解决这个问题,认为在一个优雅的方式?或者我需要使用JavaScript的?

However, I think it is not an elegant way to reach the goal. I think I have to use AJAX. How does it work? Can a usage of XHTML solve the considered problem in an elegant way? Or do I need to use JavaScripts?

我并不需要太多。我只是想有哪些(点击后),发送一些信息到服务器的一个小环节。让说我有一个明星形象附近的一个消息。如果用户点击星(他/她喜欢的消息)星改变了颜色,我的服务器更新的数据库(记住,用户喜欢的消息)。

I do not need much. I just want to have a small link which (after clicking) sent some information to server. Let say I have a "star-image" near a message. If a user click the star (he/she likes the message) star changes the color and my server update database (to remember that user likes the message).

推荐答案

如果你是完全新的AJAX(代表异步JavaScript和XML),在的 AJAX 在维基百科条目是一个良好的起点:

If you are totally new to AJAX (which stands for Asynchronous Javascript And XML), the AJAX entry on wikipedia is a good starting point:

就像DHTML和LAMP,AJAX不是技术本身,而是一组技术。 AJAX使用的组合:

Like DHTML and LAMP, AJAX is not a technology in itself, but a group of technologies. AJAX uses a combination of:

      
  • HTML和CSS来标记和   样式信息。
  •   
  • 的DOM   使用JavaScript访问到   动态显示和互动   信息presented。
  •   
  • 的方法   对于异步交换数据   浏览器和服务器,从而之间   避免页面重新加载。该   XMLHtt prequest(XHR)对象通常是   用,但有时一个IFrame对象   或动态添加标记是   替代使用。
  •   
  • 对于该数据的格式   发送给浏览器。通用格式   包括XML,pre格式的HTML,纯   文本和JavaScript对象符号   (JSON)。就可以使这种数据   动态通过某种形式的   服务器端脚本。
  •   
  • HTML and CSS for marking up and styling information.
  • The DOM accessed with JavaScript to dynamically display and interact with the information presented.
  • A method for exchanging data asynchronously between browser and server, thereby avoiding page reloads. The XMLHttpRequest (XHR) object is usually used, but sometimes an IFrame object or a dynamically added tag is used instead.
  • A format for the data sent to the browser. Common formats include XML, pre-formatted HTML, plain text, and JavaScript Object Notation (JSON). This data could be created dynamically by some form of server-side scripting.

正如你所看到的,从一个纯粹的技术问题,没有什么真正的新位置。大多数AJAX部件已经有1994年(1999年为 XMLHtt prequest )对象。真正的新奇是使用这些部件的一起的如谷歌没有与Gmail(2004年)和谷歌地图(2005年)。事实上,这两个网站贡献了大量促进AJAX的。

As you can see, from a pure technological point of view, there is nothing really new here. Most of AJAX parts were already there in 1994 (1999 for the XMLHttpRequest object). The real novelty was to use these parts together as Google did with GMail (2004) and Google Maps (2005). Actually, both sites contributed heavily to the promotion of AJAX.

有一个画面是胜过千言万语,图,说明客户机和远程服务器之间的通信下方,以及经典的Ajax应用程序之间的差异:

A picture being worth a thousand words, below a diagram that illustrates the communication between the client and the remote server, as well as the differences between the classic and the AJAX-powered applications:

有关的橙色部分,你可以用手做的一切(与 XMLHtt prequest 对象),也可以使用类似的jQuery 原型,的YUI 等,以AJAXify应用程序的客户端。这些库的目标是隐藏的JavaScript开发的复杂性(如跨浏览器的兼容性),但可能是矫枉过正一个简单的功能。

For the orange part, you can do everything by hand (with the XMLHttpRequest object) or you can use famous JavaScript libraries like jQuery, Prototype, YUI, etc to "AJAXify" the client-side of your application. Such libraries aim to hide the complexity of JavaScript development (e.g. the cross-browser compatibility), but might be overkill for a simple feature.

在服务器端,一些框架可以帮助过(如 DWR 或的RAJAX 如果您使用的是Java),但所有你需要做的基本上是公开,只返回所需的信息来部分地更新页面(服务最初是作为XML / XHTML - 在X在AJAX - 但 JSON 常常$ pferred时下p $)

On the server-side, some frameworks can help too (e.g. DWR or RAJAX if you are using Java), but all you need to do is basically to expose a service that returns only the required informations to partially update the page (initially as XML/XHTML - the X in AJAX - but JSON is often preferred nowadays).

这篇关于如何AJAX的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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