Ajax -- 获取XMLHttpRequest对象
程序员文章站
2022-07-12 18:58:20
...
<body>
<center>
<button id="btn01">获取XMLHttpRequest对象</button>
<form action="AjaxRequestServlet" method="post">
<input type="text" name="username" /><input type="submit" value="submit" />
</form>
</center>
</body>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">button {width: 200px;}</style>
<script type="text/javascript">
//获取 XMLHttpRequest
function getRequest(){
//创建XMLHttpRequest 对象
var xmlHttpRequest = null;
try{//试用与主流浏览器
xmlHttpRequest = new XMLHttpRequest();
}catch(e){//适用于IE5、IE6
try{
xmlHttpRequest = new ActiveXObejct("Microsoft.XMLHTTP");
}catch(e){
alert("your browser not support Ajax!");
}
}
return xmlHttpRequest;
};
var request = getRequest();
//页面加载完成后加载
window.onload = function(){
//document.getElementById("btn01").onclick: 获取到 btn01 点并添加点击事件
document.getElementById("btn01").onclick = function(){
alert(request);
};
};
</script>
</head>
上一篇: NullAway
下一篇: 研发、测试在工作中MOCK测试工具的使用