天天看点

互联网编程-Ajax

XMLHttpRequest 鐢ㄤ簬鍦ㄥ悗鍙颁笌鏈嶅姟鍣ㄤ氦鎹㈡暟鎹€傝繖鎰忓懗鐫€鍙互鍦ㄤ笉閲嶆柊鍔犺浇鏁翠釜缃戦〉鐨勬儏鍐典笅锛屽缃戦〉鐨勬煇閮ㄥ垎杩涜鏇存柊銆?

瀹㈡埛绔?

function invitePlayer(){
  		//Ajax鍙戦€侀個璇?  		inviting=true;
  		canBeInvited=false;
  		var xmlhttp;
  		if(window.ActiveXObject){
  			// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}else{
  			// code for IE6, IE5
  			xmlhttp=new XMLHttpRequest();
  		}
  		document.getElementById("show").innerHTML="at the begin";
  		
  		//褰搙mlhttp.send();璋冪敤姝ゆ柟娉?,鍒嗗埆璋冪敤4娆?灏辨槸readyState鏈?涓姸鎬?  		xmlhttp.onreadystatechange=sendXMLHttp;
  		document.getElementById("show").innerHTML="setfunc";
  		xmlhttp.open("GET","http://localhost:8080/ch02/invite_player.jsp?account=c"+"&id=g",true); //闇€瑕佷慨鏀?  		document.getElementById("show").innerHTML="open";
  		xmlhttp.send();
  		document.getElementById("show").innerHTML="send";
  		alert("At the end");
  	}
  	function sendXMLHttp(){
		alert("getResponese");
		if(xmlhttp.readyState==4&&xmlhttp.status==200){
			// xmlhttp.readyState=0 鏈垵濮嬪寲 锛?1  璇诲彇涓紱  2  宸茶鍙栵紱 3  浜や簰涓紱  4瀹屾垚锛?			//xmlhttp.status锛?404 琛ㄧず鏂囦欢娌℃湁鎵惧埌锛?00 鏈嶅姟鍣ㄦ暟鎹繑鍥炴垚鍔燂紱 0 琛ㄧず鏈湴鏁版嵁杩斿洖鎴愬姛锛?			//xmlDoc=xmlhttp.responseXML;
			var response=xmlhttp.responseText;
			if(response=="OK"){
				startGame();
			}
		}
	}
           

鏈嶅姟鍣ㄧ锛堜粎浠呬綔涓烘祴璇曪級锛?

<%@page contentType="text/html;charset=gbk" %>
<%@page language="java" import="java.sql.*" %>
<%
	//璁剧疆鎻愪氦琛ㄥ崟鐨勪腑鏂囩紪鐮?	request.setCharacterEncoding("GBK");
	String account,invite_id;
	account=request.getParameter("account");
	invite_id=request.getParameter("id");
	
	Connection conn=null;
    Statement stmt=null;
    ResultSet rs=null;
    
    out.print("OK");
%>
           

鍦ㄥ皾璇曠殑杩囩▼涓紝鍙戠幇鎸夌収w3c鍒涘缓XMLHttpRequest鐨勬柟娉曪紝鍦↖E鍜孍clipse鐨勬祻瑙堝櫒鎻掍欢閮藉彲浠ユ甯告墽琛岋紝浣嗘槸鍦–hrome涓細鐖嗗嚭涓嶈璇咥ctivieXObject鐨勯敊璇紝涓嶅彲鐞嗚В鍟娿€傘€傘€傘€備慨鏀规垚涓婇潰浠g爜鐨勫垱寤烘柟寮忎箣鍚庯紝鍦╟hrome涔熷彲浠ユ甯告墽琛屼簡銆?

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
           

鍦ˋjax鐨勮繑鍥炲€间腑鎬讳細鑾悕鍏跺鍦板甫鐫€鍑犱釜鎹㈣锛屽彲浠ヤ娇鐢ㄤ互涓嬫柟娉曪紝杩涜澶勭悊

鍦╦sp椤甸潰涓瘡涓€涓洖杞︽崲琛岄兘浼氫骇鐢熶竴涓┖琛岋紝聽

鍦ㄤ綘瑕佽緭鍑轰綘鎯宠鐨勫唴瀹逛箣鍓嶈皟鐢╫ut.clear();

杩欐牱杩樹笉琛岋紝灏憋細 xmlHttp.responseText.replace(/\r\n/g,"");

继续阅读