AJAX DWR
使用原始的XMLHttpRequest发出请求时,只能对Servlet和JSP操作
在JSP中创建3个function
1.createXmlHttpRequest----负责判断浏览器类型创建 XMLHttpRequest对象
var xmlHttpRequest;
function createXMLHttpRequest(){
// IE 浏览器
if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
// 非IE浏览器
return new XMLHttpRequest();
}
}
2.doLogin------负责更加创建好的XMLHttpRequest对象发出请求
function doLogin(obj){
var url = "servlet/LoginServlet?userName="+obj.value;
// 1. 创建XMLHttpRequest组件
xmlHttpRequest = createXMLHttpRequest();
// 2. 设置回调函数
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
// 3. 初始化XMLHttpRequest组件
// 使用get方法调用URL,true代表是异步的
xmlHttpRequest.open("GET",url,true);
// 4. 发送请求
xmlHttpRequest.send(null);
alert("123");
}
3.haolejiaowo---负责进行回调处理
function haoLeJiaoWo(){
// readyState-- =4表示得到了返回结果
// status=200 表示成功而且不出错
/*
请求状态:
0 -- 未初始化
1 -- 初始化
2 -- 发送请求
3 -- 开始接受结果
4 -- 接受结果完毕
每次状态改变都会调这个函数
*/
if( xmlHttpRequest.readyState == 4 &
相关文档:
用Ajax实现Tab效果的
先创建
ajax.php,在其中输入如下代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample 2_1</title>
<me ......
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Inte ......
/*在有中文参数时,接收方需要使用UTF-8方式对数据进行解码
*不支持post附件
*/
function getXmlHttpRequest() {
var xmlHttpRequest = null;
try {
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} ......
在《Pragmatic Ajax A Web 2.0 Primer 》中偶然看到对readyStae状态的介绍,感觉这个介绍很实在,摘译如下:
0: (Uninitialized) the send( ) method has not yet been invoked.
1: (Loading) the send( ) method has been invoked, request in progress.
2: (Loaded) the send( ) method has completed, entire respons ......
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:updatepanel ID="UP" runat="server">
<ContentTemplate>
......