±íµ¥ajaxÌá½»ÂÒÂëÎÊÌâ
ÎÊÌâ
ÔÚÏÖÓÐÖ÷Õ¾ÖÐʹÓõÄÊÇÊÇGBK±àÂëµÄ£¬µ±±íµ¥Ê¹ÓÃform submit·½Ê½µÝ½»µÄ»°Ã»ÓÐÎÊÌ⣬·þÎñÆ÷¶ËÄܹ»Õýȷʶ±ð×Ö·û±àÂë¡£µ«ÊÇ£¬µ±¿Í»§¶ËʹÓÃajaxµÄ·½Ê½µÝ½»±íµ¥µÄ»°£¬·þÎñÆ÷¶Ëʶ±ð¿Í»§¶ËµÝ½»µÄ±íµ¥µÄÄÚÈÝ£¬µ±ÄÚÈÝÖÐÓÐÖÐÎÄ×Ö·ûµÄ»°¾Í»á³öÏÖÂÒÂëÏÖÏó¡£
ÔÒò·ÖÎö
¾¿ÆäÔÒò£¬ÆäʵºÜ¼òµ¥£¬Ê¹ÓÃajax·½Ê½Ìá½»µÄ±íµ¥ÊÇÓÃutf-8±àÂëÀ´Ìá½»µÄ£¬ÕâÑù·þÎñÆ÷¶ËÔÚ½ÓÊÕ¿Í»§¶Ë±íµ¥µÄÄÚÈݵÄʱºò»¹ÊÇ°´ÕÕGBK»òÆäËû±àÂ뷽ʽÀ´½âÎöµÄ£¬×ÔÈ»½âÎö³öÀ´µÄÄÚÈÝ»á³öÏÖÂÒÂëÁË¡£
±íµ¥ÄÚÈÝ¿ÉÒÔͨ¹ýGET»òPOST·½Ê½·¢ËÍ£¬µ«ÊÇÓÃÕâÁ½ÖÖ·½Ê½Óм¸¸ö²»Í¬Ö®´¦£¬¶Ô“ÄãºÃ”µ÷ÓÃjavascriptº¯ÊýencodeURI£¨£©½âÎöÖ®ºóµÃµ½µÄ×Ö·û´®Îª“%E4%BD%A0%E5%A5%BD”£¬ÐèҪעÒâµÄÒ»µã£¬encodeURI£¨£©Õâ¸öº¯ÊýÖ»»á¶Ô×Ö·û´®½øÐÐutf8±àÂë¡£µ±¿Í»§¶Ë°Ñ“%E4%BD%A0%E5%A5%BD”Õâ¶Î×Ö·û´®Í¨¹ýGET»òÕßPOST·½Ê½·¢ËͻᷢÉúʲôÊ¡£
1.GET·½Ê½
µ±ÓÃget·½Ê½Ìá½»±íµ¥£¬ÔÚ·þÎñ¶ËµÄtomcat·þÎñÆ÷ÖлáÓÃÒ»¸öconnectorÀ´×Ô¶¯½âÎö±àÂëÖ®ºó×Ö·û´®£¬ÒÔÏÂÊÇconnectorµÄÅäÖýڵ㣺
<Connector port="80"
maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false" redirectPort="8443"
acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="GBK" />
ÉÏÃæÅäÖÃÖÐÓÐÒ»¸öÊôÐÔURIEncoding="GBK"µÄ×÷ÓÃÊÇ£¬µ±¿Í»§¶Ëget·¢ËÍÐÅÏ¢ÖÐÓÐencodeģʽµÄ×Ö·û´®¾ÍÊÇÓÃgbkÀ´decodeËü¡£ºÍÒź¶£¬Ö÷Õ¾·þÎñÆ÷ÖеÄURIEncodingÊôÐÔÊÇgbk²»ÄܱäÁË£¬¶ø¿Í»§¶ËencodeURI£¨£©Ö»ÄܱàÂëUTF-8µÄ×Ö·û¡£ËùÒÔ£¬ÓÃÕâÖÖ·½Ê½·¢Ë͵Ä×Ö·û´®ÔÚ·þÎñÆ÷¶Ë¾Í»á³öÏÖÂÒÂëÏÖÏó¡£
2. POST·½Ê½
µ±¿Í»§¶Ë·¢ËÍ“%E4%BD%A0%E5%A5%BD”×Ö·û´®Ê±£¬ÊÇͨ¹ýYAHOO.util.ConnectÕâ¸ö¿Ø¼þÀ´·¢Ë͵ģ¬Èç¹û¿Í»§¶ËÄÜÕý³£µØ·¢ËÍ“%E4%BD%A0%E5%A5%BD”Õâ¸ö×Ö·û´®£¬·þÎñ¶Ë½ÓÊÕµ½Ö®ºó²¢²»»á×ö¶îÍâµÄתÒ壬µ«ÊÇYAHOO.util.Connect ½«encodeÖ®ºó×Ö·û´®´«¸øConnect¿Ø¼þÖ®ºó£¬Õâ¸ö¿Ø¼þÔÚÄÚ²¿»á½«encodeÖ®ºóµÄ×Ö·ûÔÙdecodeÖ®ºóÔÙ·¢ËÍ£¬ÒòΪ·¢Ë͵ÄÊÇutf-8¸ñʽµÄÄÚÈÝ£¬ËùÒÔ·þÎñÆ÷¶Ë¾Í»á³öÏÖÂÒÂë¡£
½â¾ö°ì·¨
³¢ÊÔÁËÒÔÉÏgetºÍpost·½Ê½Ö®ºó£¬¾õµÃÔÚÇëÇóµÄURLÉÏ·¢ËÍÊý¾ÝÂÒÂëÎÊÌâµÄ½â¾ö·½°¸±È½ÏÂé·³£¬¿öÇÒ£¬httpÐÒéÖÐÒѾÃ÷ȷ˵ÁËgetºÍpostµÄÇø±ð£¬getÊÇ´Ó·þÎñÆ÷¶ËÄÃһЩ¶«Î÷£¬¶øpostÊÇÏò·þÎñÆ÷´«Ê䶫Î÷¡£get·½Ê½Ïò·þÎñÆ÷¶Ë·¢Ë
Ïà¹ØÎĵµ£º
<script language="javascript" type="text/javascript">
var xmlHttp = null;
function CreateXMLHttp()
{
......
±¾ÎÄÄÚÈÝÕª×Ô£º¡¶Java Web¿ª·¢½Ì³Ì——ÈëÃÅÓëÌá¸ßƪ£¨JSP+Servlet£©¡·
½»»¥µÄ»ù±¾¹ý³Ì°üÀ¨£º
l ´´½¨XMLHttpRequest¶ÔÏó£»
l ·¢ËÍÇëÇó£»
l ´¦ÀíÏìÓ¦¡£
´ ......
/*
κÑÔAJAX Àà
ʹÓ÷½·¨:
<mce:script type="text/javascript"><!--
var classAjax = new Ajax(sUrl,sRecvTyp,sQueryString,oResultFunc);
classAjax.post();//post·½Ê½·¢ËÍÊý¾Ý
classAjax.get();//get·½Ê½·¢ËÍÊý¾Ý
//sUrl·¢Ë͵ØÖ·
//sRecvTyp ½ÓÊÜÊý¾ÝÀàÐÍ:0Ϊxml 1Ϊtext
//oResultFunc ·µ»Ø½á¹û´¦Àíº ......
µ±ÓÃajax·µ»Ø¶à¸öÖµµÄʱºò£¬¿ÉÒÔÓÃxml£¬Ò²¿ÉÒÔÓÃjson
jsonʵÀý
<html>
<head>
<title>AjaxTest</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
& ......
ÎÒÓÃActiveXObject("Microsoft.xmldom")À´×°ÔØxmlÎĵµÊ±£¬¾¹È»ÀÏÊÇÌáʾװÔØʧ°Ü£¬ºóÀ´ÉÏÍøËÑË÷ÁËÏ£¬ËµÊDz»ÄÜÖ±½Ó°üº¬Õâ5¸ö·ûºÅ£¨'><&"£©£¬±ØÐëÑ¡ÓÃ&xx;·½Ê½À´×ªÒ壬¿ÉÎÒÕÒ±éXMLҲû·¢ÏÖÕâЩ¡£
ºóÀ´ÎÒ¶ÀÐÐÅųý£¬²Å·¢ÏÖÓÐÒ»¸öÔªËصÄÊôÐÔֵΪÓГ|”£¬È¥³ýºó¾ÍÕý³£ÁË¡£ÎÒÕæÊǾõµÃÆæ¹Ö£¬ÍøÉÏҲûÓÐ˵ ......