ajax -第一個例子
1.MyHtml.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
?<script type='text/javascript'>
??var req = null;
??var console = null;
??var READY_STATE_UNINITIALIZED = 0;
??var READY_STATE_LOADING = 1;
??var READY_STATE_LOADED = 2;
??var READY_STATE_INTERACTIVE = 3;
??var READY_STATE_COMPLETE = 4;
??
??function sendRequest(url,params,HttpMethod){
???if(!HttpMethod){
????HttpMethod = "GET";
???}
???req = initXMLHTTPRequest();
???if(req){
????req.onreadystatechange = onReadyState;
????req.open(HttpMethod,url,true);
????req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
????req.send(params);
????
???}
??}
??
??function initXMLHTTPRequest(){
???var xRequest = null;
???if(window.XMLHttpRequest){
????xRequest = new XMLHttpRequest();
???}else if(window.ActiveXObject){
????xRequest = new ActiveXObject("Microsoft.XMLHTTP");
???}
???return xRequest;
??}
??
??function onReadyState(){
???var ready = req.readyState;
???var data = null;
???if(ready==READY_STATE_COMPLETE){
????data = req.responseText;
???}else{
????data = "loading...[" + ready +"]";
???}
???toConsole(data);
??}
??
??function toConsole(data){
???if(console!=null){
????var newline = document.createElement("div");
????console.appendChild(newline);
????var txt = document.createTextNode(data);
????console.appendChild(txt);
???}
??}
??
??window.onload = function(){
???console = document.getElementById('console');
???sendRequest("data.txt");
??}
?</script>
? </head>
? ?
? <body>
? ?<DIV id ='console'></DIV>
? </body>
</html>
2.data.txt
?i'm ok!!!!!!!!
posted on 2006-08-11 14:46 software5168 閱讀(430) 評論(1) 編輯 收藏 所屬分類: Java學習