ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAX ʵÀý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

AJAX
AJAX ʵÀý
AJAXä¯ÀÀÆ÷
AJAXÔ´´úÂë
AJAX·þÎñ¶Ë
AJAX Êý¾Ý¿â
AJAXÓëXMLÎļþ
AJAX XMLHttpRequ..
΢ÈíµÄAjax
AJAX ½éÉÜ
AJAX HTTP ÇëÇó
AJAX ·þÎñÆ÷¶Ë½Å±..
AJAX Suggest °¸À..
AJAX ResponseXML
AJAX AppML

AJAX ʵÀý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-06-02 ä¯ÀÀ:10200 :: ::

AJAX can be used to create more interactive applications.
AJAX¿ÉÒÔÓÃÀ´´´½¨½»»¥Ê½ÐÔ¸üºÃµÄÍøÂçÓ¦ÓóÌÐò¡£


AJAX Example
AJAX ʵÀý

In the AJAX example below we will demonstrate how a web page can communicate with a web server online as a user enters data into a web form.
ÔÚÒÔϵÄAJAX·¶ÀýÖУ¬ÎÒÃǽ«Á˽⵽µ±Óû§ÒÔÍøÒ³¸ñʽÊäÈëÊý¾Ýʱһ¸öÍøÒ³ÊÇÈçºÎÓëÍøÂç·þÎñÆ÷Á¬½ÓµÄ¡£


Type a Name in the Box Below
ÔÚÏÂÃæµÄ¿òÖÐÊäÈëÐÕÃû

First Name:

Suggestions:


Example Explained - The HTML Form
ʵÀý½âÎö-³¬Îı¾±ê¼ÇÓïÑÔ±íµ¥

The form above has the following HTML code:
ÒÔÉϵķ¶ÀýËùº¬³¬Îı¾±ê¼ÇÓïÑÔ´úÂëÈçÏ£º

<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

As you can see it is just a simple HTML form with an input field called "txt1".
¾ÍÈçÄã¿´µ½µÄ£¬ËüÖ»ÊÇÒ»¸öÆÕͨµÄ±íµ¥£¬ÀïÃæÓÐÒ»³ÆÎª"txt1"µÄÊäÈë¿ò

An event attribute for the input field defines a function to be triggered by the onkeyup event.
ÔÚinputÇøÓòÀïÓÐÒ»¸öʼþÊôÐÔ£¬µ±°´¼üÊͷŵÄʱºò¾Í´¥·¢Õâ¸öʼþ¡£

The paragraph below the form contains a span called "txtHint". The span is used as a placeholder for data retrieved from the web server.
ÏÂÒ»¶Î°üÀ¨ÁËÒ»¸ö³Æ×ö“txtHint”µÄSPAN¡£Õâ¸öSPANÊÇÓÃÀ´´æ´¢´Ó·þÎñÆ÷ÖØÐ»ñµÃµÄÐÅÏ¢µÄ¡£

When the user inputs data, a function called "showHint()" is executed. The execution of the function is triggered by the "onkeyup" event. In other words: Each time the user moves his finger away from a keyboard key inside the input field, the function showHint is called.
µ±Óû§ÊäÈëÊý¾Ý£¬ÃûΪ“showHint()”µÄº¯Êý½«±»Ö´ÐС£Õâ¸öº¯ÊýµÄÖ´ÐÐÊÇÓÉ“onkeyup”ʼþ´¥·¢µÄ¡£»»ÖÖ˵·¨£ºÃ¿µ±Óû§ÔÚtxt1ÇøÓòÄÚ´¥¶¯¼üÅ̰´Å¥£¬showHintÕâ¸öº¯Êý¾Í»áÖ´ÐС£


Example Explained - The showHint() Function
ʵÀý½âÎö- showHint()º¯Êý

The showHint() function is a very simple JavaScript function placed in the <head> section of the HTML page.
showHint()º¯ÊýÊÇÒ»ÖÖλÓÚHTML¶¥¶ËµÄ¼òµ¥µÄJSº¯Êý¡£

The function contains the following code:
º¯Êý°üº¬ÒÔÏ´úÂ룺

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML=""

return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="gethint.asp"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

The function executes every time a character is entered in the input field.
ÿµ±ÓÐ×Ö·û±»¼üÈëÊäÈëÇøÄھͻáÖ´ÐÐÕâ¸öº¯Êý

If there is some input in the text field (str.length > 0) the function executes the following:
ÈçÓÐ×Ö·û±»ÊäÈëÎÄ×ÖÊäÈëÇø(str.length>0)º¯Êý¾ÍÖ´ÐУº

  • Defines the url (filename) to send to the server
    ¶¨ÒåURL(ÎļþÃû)·¢Ë͵½·þÎñÆ÷

  • Adds a parameter (q) to the url with the content of the input field
    ͨ¹ýÊäÈ뽫ÄÚÈݸ½¼Óµ½²ÎÊý(q)ºó

  • Adds a random number to prevent the server from using a cached file
    Ìí¼ÓÒ»¸öËæ»úÊýÀ´±£»¤·þÎñÆ÷£¬±ÜÃâ¶ÁÈ¡»º´æ

  • Creates an XMLHTTP object, and tells the object to execute a function called stateChanged when a change is triggered
    ½¨Á¢Ò»¸öXMLHTTP¶ÔÏ󣬵±HTTP´¥·¢Ò»´Î±ä¶¯ÔòXMLHTTP¶ÔÏó¾Í»áÖ´ÐÐstateChanged()º¯Êý

  • Opens the XMLHTTP object with the given url.
    ͨ¹ý¸ø¶¨µÄurlÀ´´ò¿ªXMLHTTP¶ÔÏó

  • Sends an HTTP request to the server
    ½«HTTPÇëÇó·¢Ë͵½·þÎñÆ÷ÉÏ

If the input field is empty, the function simply clears the content of the txtHint placeholder.


Example Explained - The stateChanged() Function
ʵÀý½âÎö - stateChanged()º¯Êý

The stateChanged() function contains the following code:
stateChanged()º¯Êý°üº¬ÒÔÏ´úÂ룺

function stateChanged() 
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

The stateChanged() function executes every time the state of the XMLHTTP object changes.
ÿµ±XMLHTTP¶ÔÏóµÄ״̬·¢Éú¸Ä±ästateChanged()º¯Êý¾Í»á±»Ö´ÐÐ

When the state changes to 4 (or to "complete"), the content of the txtHint placeholder is filled with the response text.
µ±×´Ì¬¸Ä±äΪ4(»òΪ"Íê³É"),txtHint spanÀï¾Í»áÏÔʾ·´À¡µÄÎÄ×Ö

ÆÀÂÛ (17) 1 2 3 4 All