admin管理员组文章数量:1134247
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?
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.
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).
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?
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.
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).
Share Improve this question edited Oct 2, 2009 at 15:17 Ryan Doherty 38.7k4 gold badges57 silver badges62 bronze badges asked Oct 2, 2009 at 14:51 VerrtexVerrtex 4,49710 gold badges37 silver badges33 bronze badges 2- 8 Obligatory link to wikipedia: en.wikipedia.org/wiki/Ajax_(programming) – Jon B Commented Oct 2, 2009 at 14:56
- For the answer please visit the link: w3schools.com/php/php_ajax_intro.asp – user2215741 Commented Mar 27, 2013 at 13:13
8 Answers
Reset to default 123If you are totally new to AJAX (which stands for Asynchronous Javascript And XML), the AJAX entry on wikipedia is a good starting point:
Like DHTML and LAMP, AJAX is not a technology in itself, but a group of technologies. AJAX uses a combination of:
- 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.
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.
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:
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.
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).
The essence of AJAX is this:
Your pages can browse the web and update their own content while the user is doing other things.
That is, your javascript can send asynchronous GET and POST requests (usually via an XMLHttpRequest
object) then use the results of those requests to modify its page (via Document Object Model manipulation).
AJAX typically involves sending HTTP requests from client to server and processing the server's response, without reloading the entire page. (Asynchronously).
Javascript generally does the submission and receives the data response from the server (traditionally XML, often other less verbose formats like JSON)
The Javascript then may update the page DOM dynamically to update the user's view.
Thus 'Asychronous Javascript And XML'.
There are other options to update the user's view without reloading the page, things like Flash and Applets, but these don't sound like good solutions for your case. Sounds like Javascript is the way to go. There's loads of good library support around, like jQuery as is used on this site, so you don't need to actually write much Javascript yourself.
Ajax is more than reload just a part of the page. Ajax stands for Asynchronous Javascript And Xml.
The only part of Ajax that you need is the XMLHttpRequest object from javascript. You have to use it to load and reload small part of your html as a div or any other tags.
Read that example and you will be pro sooner as you think!
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
document.myForm.time.value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","time.asp",true);
xmlhttp.send(null);
}
</script>
<form name="myForm">
Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
Time: <input type="text" name="time" />
</form>
</body>
</html>
AJAX stands for Asynchronous Javascript and XML. AJAX supports partial updates to pages without having to post the entire page back to the server.
There are plenty of options for AJAX. The two most notable (arguably) are Microsoft's ASP.NET AJAX (formerly Atlas) and jQuery.
ASP.NET AJAX is relatively easy to set up if you're already familiar with ASP.NET. jQuery is good if you already know javascript, and allows very granular control over the querying and updating of your page.
HTH
If you're interested, IBM have a 10 (possibly more) part series on Ajax: Mastering Ajax part 1
Although a few years old now it's a good intro, (even if you just read the first part!)
I think the whole series should be listed Here, although the site's being a bit slow for me at the moment...
Summary:
Ajax, which consists of HTML, JavaScript™ technology, DHTML, and DOM, is an outstanding approach that helps you transform clunky Web interfaces into interactive Ajax applications. The author, an Ajax expert, demonstrates how these technologies work together -- from an overview to a detailed look -- to make extremely efficient Web development an easy reality. He also unveils the central concepts of Ajax, including the XMLHttpRequest object.
that is ajax. you cannot use ajax without javascript. you should look at jquery and prototype examples to get an idea of usage.
What you are trying to do is technically ajax. Ajax creates xhtml fragment transactions to update sections of a page. Javascript makes these get requests nice and neat.
本文标签: javascriptHow does AJAX workStack Overflow
版权声明:本文标题:javascript - How does AJAX work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736856636a1955740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论