JS瀹炵幇AJAX璇锋眰_2锛堣繘闃剁瘒锛?/h1>
闂锛?/span>褰撳娆¤皟鐢ㄥ悓涓€涓狝JAX璇锋眰鏃讹紝鍙兘浼氶亣鍒版湇鍔″櫒鐨勬暟鎹彂鐢熸敼鍙樹簡锛屼絾鏄湪娴忚鍣ㄤ腑鑾峰彇鐨勬暟鎹病鏈夊悓姝ョ殑闂銆傞棶棰樼殑鍘熷洜鍦ㄤ簬娴忚鍣ㄧ殑缂撳瓨銆傝繖绉嶆儏鍐典竴鑸彧鍙戠敓鍦℅ET璇锋眰涓€?/p>
聽 浣跨敤鏃犳剰涔夌殑鍙傛暟閬垮厤缂撳瓨锛?/p>
聽 鍦ㄦ湇鍔$娣诲姞鍝嶅簲澶达細 聽 聽闂锛?/span>褰撴垜浠娇鐢℅ET璇锋眰浼犲弬涓湁涓枃鏃讹紝浼氬嚭鐜颁贡鐮佺殑鎯呭喌锛孭OST璇锋眰杞爜姣旇緝绠€鍗曘€?/p>
聽 GET璇锋眰锛?/p>
聽 聽POST璇锋眰锛?/p>
聽 聽 瀵规墍鏈夌殑璇锋眰涔犳儻鎬у湪servlet涓兘璁剧疆缂栫爜鏍煎紡 聽 request.setCharacterEncoding("UTF-8"); 聽聽response.setCharacterEncoding("UTF-8"); GET璇锋眰涓湁涓枃鏃讹紝灏嗕腑鏂囧寘瑁硅捣鏉ワ紝鑻辨枃涔熷彲浠ュ寘瑁癸紝娌℃湁浠€涔堝奖鍝嶃€?/span>閬垮厤GET缂撳瓨锛?/h2>
//杩欓噷鍙拡瀵归潪IE鐨勬祻瑙堝櫒浜嗭紝IE涔熸槸濡傛锛屽彧鏈夊璞$殑鍒涘缓涓嶅悓缃簡
var xmlHttp=new XMLHttpRequest();
var timer=new Date().getTime();//鑾峰彇褰撳墠鐨勬椂闂达紝姣忔璇锋眰鏃剁殑鏃堕棿鏄笉鍚岀殑
xmlHttp.open("GET","ajax.do?temp="+timer,true);
xmlHttp.send();
//鍦╯ervlet涓坊鍔犲搷搴斿ご
response.setContentType("text/html;charset=UTF-8");
response.addHeader("pragma","no-cache");
response.addHeader("cache-control","no-cache");
response.addHeader("expires","0");
//杩欎笁涓搷搴斿ご鍛婅瘔娴忚鍣ㄤ笉浣跨敤缂撳瓨
AJAX瀵逛腑鏂囩殑鏀寔锛?/h2>
//js浼犱腑鏂?
var xmlHttp=new XMLHttpRequest();
var v=encodeURIComponent("杩欐槸姹夊瓧");//灏嗕腑鏂囧寘瑁硅捣鏉?
xmlHttp.open("GET","ajax.do?param="+v);
xmlHttp.send();
//servlet涓?
String v=request.getParameter("param");//鎺ユ敹璇锋眰鍦板潃涓殑param鐨勫€?
v=new String(v.getBytes("ISO-8859-1"),"UTF-8");//灏嗗師鏁版嵁閲嶆柊缂栫爜涓篣TF-8鏍煎紡
//js
var xmlHttp=new XMLHttpRequest();
xmlHttp.open("POST","ajax.do");
xmlHttp.send("v=杩欐槸姹夊瓧");//涓嶇敤鍖呰9璧锋潵
//servlet涓?
request.setCharacterEncoding("UTF-8");//杩欓噷璁剧疆璇锋眰鐨勭紪鐮佹牸寮忓嵆鍙?
String v=request.getParameter("v");//v=杩欐槸姹夊瓧
鎬荤粨锛?/h2>
缁撹锛?/h2>