AJAX Basic
AJAX基础
AJAX HOME
AJAX首页
AJAX Intro
AJAX简介
AJAX Request
AJAX请求
AJAX Example
AJAX实例
AJAX Browsers
AJAX浏览器
AJAX XMLHttpRequest
AJAX XMLHttpRequest对象
AJAX Server
AJAX服务器端
AJAX Server Script
AJAX服务器脚本
AJAX Advanced
AJAX高阶
AJAX Suggest
AJAX建议
AJAX Source
AJAX源码
AJAX Database
AJAX数据库
AJAX XML File
AJAX XML文件
AJAX ResponseXML
AJAX响应XML
AJAX Examples
AJAX范例
AJAX - Request a Server
AJAX-请求服务器
AJAX - Sending a request to a server
AJAX-向服务器端发送请求
To send off a request to the server, we use the open() and send() methods.
我们利用open()和send()方法向服务器发送请求。
The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously.
open()方法带有3个参数。第一个参数定义我们发送请求的方法类型(GET或POST)。第二个参数指定服务器端处理脚本的URL。第三个参数指定异步处理的请求。
The send() method sends the request off to the server. If we assume that the HTML and ASP file are in the same directory, the code would be:
send()方法发送请求至服务器端。如果假设我们把HTML和ASP文件放在同一目录下,代码就应该是:
| xmlhttp.open("GET","time.asp",true); xmlhttp.send(null); |
Now we must decide when the AJAX function should be executed.
现在我们必须决定什么时候开始让AJAX函数执行了。
We will let the function run "behind the scenes" when a user types something in the "Name" field:
当我们在“Name”字段输入信息时,我们将让函数“在幕后”运行。
| <form name="myForm"> Name: <input type="text" name="username" onkeyup="ajaxFunction();" /> Time: <input type="text" name="time" /> </form> |
Our updated "testAjax.htm" file now looks like this:
我们现在更新“testAjax.htm”文件如下:
| <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> |
The next chapter makes our AJAX application complete with the "time.asp" script.
下一章节我们将用“time.asp”脚本完成AJAX程序。
