AjaxʹÓÃPOSTÌá½»ÖÐÎÄÂÒÂëÎÊÌâ(ת)
Ç°¶Îʱ¼äдJSP£¬Ê¹ÓÃAJAXÒÔPOST·½Ê½Ìá½»Êý¾Ý£¬Èç¹ûÊÇÖÐÎÄ×Ö·ûÌá½»¾Í»áÂÒÂ룬ºóÀ´Ð´ASPʱÓõ½AJAXÒÔPOST·½Ê½Ìá½»Êý¾Ý£¬ÖÐÎÄÒ»ÑùÊÇÂÒÂë¡£ËÑË÷Ò»ÏÂÏà¹Ø×ÊÁÏ£¬ÎÊÌâÓ¦¸ÃÊÇÌá½»Êý¾ÝʱÊÇÒÔUTF-8±àÂëÌá½»£¬ËùÒÔ½ÓÊÕʱÈç¹ûʹÓÃGB2312»òÕßÆäËüÖÐÎıàÂëµÄ»°¾Í»áÂÒÂë¡£
ʹÓÃGET·½Ê½Ìá½»Êý¾ÝµÄʱºò£¬ÖÐÎÄÎÊÌâºÜºÃ½â¾ö£¬setRequestHeader("Content-Type","text/html; encoding=gb18030")¾Í¿ÉÒÔÁË¡£µ«Õâ¸ö·½·¨ÔÚPOST·½Ê½ÖÐÈ´²»Æð×÷Óᣴó¼Ò¶¼ÖªµÀGET·½Ê½Ìá½»Êý¾ÝÓ㤶ÈÏÞÖÆ£¬ÓÐʱÎÒÃDZØÐëʹÓà POST·½Ê½À´Ìá½»Êý¾Ý¡£
¶ÔÓÚʹÓÃPOST£¬JSPµÄ½â¾ö·½·¨ÈçÏ£º
ʹÓÃescape£¨»òencodeURI£¬Á½¸öº¯Êý¶¼ÊÇJavaScriptµÄº¯Êý£¬¹¦ÄÜ»ù±¾Ïàͬ£¬¿ÉÒÔ²éÒ»ÏÂÏà¹ØµÄ°ïÖú£©£¬µ«ÒªÊ¹ÓÃÁ½´Î£¬Õâ¸öÊǹؼü¡£
³õʼҳÃæÄÚÈÝÈçÏÂ(hello.jsp)£º
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AJAXÌá½»Ò³Ãæ</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<script type="text/javascript">
function justdo(){
var post = "name=ÍõÁ¦ÃÍ&email=wallimn@sohu.com&bokee=http://wallimn.bokee.com";
post = encodeURI(post);
post = encodeURI(post); //Á½´Î£¬ºÜ¹Ø¼ü
var xmlObj = new ActiveXObject("Msxml2.XMLHTTP");
var URL = "act.jsp"; //ÎļþÃûÐèÒªµ÷Õû³É²âÊÔʱµÄÏàӦλÖÃ
xmlObj.open("POST",URL,true);
xmlObj.setRequestHeader("Cache-Control","no-cache");
xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlObj.send(post); //×¢Ò⣺POST·½Ê½£¬Ê¹ÓÃÕâ¸öÀ´·¢ËÍÄÚÈÝ
}
</script>
</head>
<body>
<input type="button" value="Ìá½»" onclick="justdo()" />
</body>
</html>
AjaxÇëÇó´¦ÀíÒ³Ãæ(act.jsp)µÄÄÚÈÝÈçÏÂ:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "
Ïà¹ØÎĵµ£º
Ò»°ãÀ´Ëµ£¬ÄãÖ»ÐèÒªÒ»¸ödwr.xmlÎļþ£¬²¢ÇÒ·ÅÖÃÔÚĬÈϵÄλÖãºWEB-INF/dwr.xml¡£ Èç¹ûÄÇÑùµÄ»°£¬Äã¿ÉÒÔ²»ÓÃÁ˽âÏÂÃæµÄÅäÖá£
ÓÐÈý¸öÔÒòʹÄãÏ£ÍûÖ¸¶¨²»Í¬Î»ÖõÄdwr.xmlÎļþ¡£
ÄãÏ£ÍûÈÃdwr.xmlÎļþºÍËüÄÜ·ÃÎʵ½µÄ×ÊÔ´ÔÚÒ»Æð¡£ÔÚÕâÖÖÇé¿öÏÂÄãÐèÒªÒ»¸öÕâÑùµÄÅäÖ㺠<param-value>WEB-INF/classes/com/yourco/dwr/dwr. ......
¡¡Ò»°ãÇé¿öÏ£¬Ê¹ÓÃAjaxÌá½»µÄ²ÎÊý¶àÊÇЩ¼òµ¥µÄ×Ö·û´®£¬¿ÉÒÔÖ±½ÓʹÓÃGET·½·¨½«ÒªÌá½»µÄ²ÎÊýдµ½open·½·¨µÄurl²ÎÊýÖУ¬´Ëʱsend·½·¨µÄ²ÎÊýΪnull¡£
ÀýÈç £º
¡¡¡¡ var url = "login.jsp?user=XXX&pwd=XXX";
¡¡¡¡xmlHttpRequest.open("GET",url,true);
¡¡¡¡xmlHttpRequset.send(null);
¡¡¡¡´ËÍ⣠......
//Óû§ÃûУÑéµÄ·½·¨
//Õâ¸ö·½·¨½«Ê¹ÓÃXMLHTTPRequest¶ÔÏóÀ´½øÐÐAJAXµÄÒì²½Êý¾Ý½»»¥
var xmlhttp;
function verify() {
//0¡£Ê¹ÓÃdomµÄ·½Ê½»ñÈ¡Îı¾¿òÖеÄÖµ
//document.getElementById("userName")ÊÇdomÖлñÈ¡ÔªËؽڵãµÄÒ»ÖÖ·½·¨£¬Ò»¸öÔªËؽڵã¶ÔÓ¦HTMLÒ³ÃæÖеÄÒ»¸ö± ......
¶ÔÏëѧAjaxµÄÐÂÈË£¬Ç¿ÁÒÍƼöÒ»ÏÂÎÄÕ£º¡¶ÕÆÎÕ Ajax ϵÁС· http://www.ibm.com/developerworks/cn/web/wa-ajaxintro/
¸ÃϵÁУ¬²»ÏñÄÇЩͷʹҽͷ½ÅÍ´Ò½½ÅµÄËÙʳÅÉ£¬ßëßë߶߶˵ÁË°ëÌ춼¾õµÃû˵µ½µã×ÓÉÏ£¬¸ÃϵÁÐÎÄÕÂÉîÈëdz³öµÄ½éÉÜÁËһЩºÜ»ù±¾µÄ֪ʶ£¬µÀ³öÁËһЩ·Ç³£±¾ÖʵÄÄںˣ¬¾ÍËãÄãÊÇAjaxÀÏÊÖ£ ......