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 Examples
AJAX范例

AJAX - Request a Server

AJAX-请求服务器

Previous Next

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程序。


Previous Next

宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有