ÈçºÎ¿ª·¢°²È«µÄAJAXÓ¦ÓÃ
AJAX¼¼ÊõÒѾÊÇÏÖÔÚ×îÁ÷ÐеÄWebÓ¦Óÿª·¢¼¼ÊõÁË£¬µ«ÊÇÓë´Ëͬʱ£¬WebÓ¦ÓÃÒ²³ÉÁËÕâ¸öIT¼Ü¹¹Öа²È«×Èõ£¬×îÈÝÒ×Êܵ½¹¥»÷µÄ²¿·Ö£¬AJAXÓ¦ÓÃÏà±È½ÏÓ봫ͳµÄWebÓ¦Ó㬴ó´óÔö¼ÓÁË¿Í»§¶ËÓë·þÎñÆ÷Ö®¼äµÄ½»»¥£¬Í¬Ê±Ò²Ê¹µÃһЩºǫ́µÄÒµÎñÂß¼½Ó¿Ú±©Â¶¸øÁË¿Í»§¶Ë£¬Èç¹û·þÎñÆ÷¶ËûÓÐ×ã¹»µÄ±£»¤»òÕßûÓжԿͻ§¶ËÇëÇó½øÐкϷ¨ÐÔУÑ飬¹¥»÷Õ߾ͻá³ÃÐé¶øÈ룬½øÈëϵͳÄÚ²¿½øÐÐÆÆ»µ¡£¿ª·¢ÈËÔ±ÈçºÎ²ÅÄÜÔÚ¿ª·¢¹¤³ÌÖб£Ö¤AJAXÓ¦ÓõݲȫÄØ£¿ Ò»ÏÂÊÇÎÒËѼ¯×ÊÁÏ×ܽá³öÀ´µÄһЩchecklistºÍbest practices£¬Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú¡£
1. ÊäÈëУÑ飬ÕâÒ»²¿·ÖÒѾÔÚÎÒµÄÉÏһƪblog ÈçºÎ½øÐÐWebÓ¦Óõݲȫ²âÊÔºÍÊäÈëУÑé ÖнøÐÐÁË˵Ã÷£¬½øÐÐÊäÈëУÑéÓÐÁ½ÖÖ·½Ê½£¬Ò»ÖÖÊÇBlacklisting: ¾ÍÊÇÁгöËùÓзǷ¨µÄÊäÈë½øÐÐÆÁ±Î£»ÁíÍâÒ»ÖÖÊÇWhitelisting: ¾ÍÊÇÁгöºÏ·¨µÄÊäÈë¸ñʽ£¬Ö»Òª²»ÊôÓÚÕâÖÖ¸ñʽ¶¼»®Îª·Ç·¨¸ñʽ½øÐÐÆÁ±Î¡£°²È«·½ÃæÀ´Ëµ£¬Whitelisting±ÈBlacklistingÓиü¸ßµÄ°²È«ÐÔ¡£
2. ¾¡Á¿±ÜÃ⶯̬µÄÉú³ÉºÍÖ´ÐÐcode, ÔÚjavascriptÖо¡Á¿±ÜÃâʹÓÃevalº¯Êý¡£
3. ÔÚʹÓÃjson¶ÔÏó֮ǰ¶ÔËü½øÐÐУÑ飬ÒòΪjson¶ÔÏóÒ²ÊÇjavascriptµÄÒ»²¿·Ö£¬ËùÒÔjson¶ÔÏóÀïÃæÒ²ÓпÉÄÜ°üº¬ÓÐÓк¦µÄ´úÂë£ ......
NO1.
ÔÚatlas(ajax)ÖÐÇ벻ҪʹÓÃResponse.Write(),
ÇëʹÓÃClientScript.RegisterClientScriptBlockÌæ»»
ʹÓÃÈçÏ£º
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('×Ô¼º¿´×Å°ì!')</script>");
×¢Ò⣺RegisterStartupScript ·½·¨Êǽ« JavaScript ǶÈëµ½ ASP.NET Ò³ÃæµÄµ×²¿£¬Ç¡ºÃλÓڹرÕÔªËØ </form> µÄÇ°Ãæ¡£RegisterClientScriptBlock ·½·¨Êǽ« JavaScript ǶÈëµ½Ò³ÃæÖпªÆôÔªËØ <form> µÄ½ôºóÃæ¡£
NO2.
FilteredTextBox¿Ø¼þÊÇÒ»¸ö·Ç³£ÓÐÓõĿؼþ£¬Ê¹ÓÃËûÄÜÖ±½Ó¹ýÂËһЩ×Ö·û¡£
Àý×Ó£º
Ò»£ºÖ»ÔÊÐíÊý×Ö
<asp:TextBox ID="TextBox1" runat="server" />
<ajaxToolkit:FilteredTextBoxExtender
ID="FilteredTextBoxExtender1"
runat="server"
TargetControlID="TextBox1"
FilterType="Numbers" />
¶þ£ºÖ»ÔÊÐíÊäÈëС×Ö×Öĸ
< ......
<style type="text/css">
<!--
.STYLE1 {
font-size: 24px;
font-weight: bold;
}
.STYLE2 {font-size: 36px}
-->
.mouseOut {
color: #000000;
}
.mouseOver {
color: RED;
}
.mouseOut1 {
background: #708090;
color: #FFFAFA;
}
.mouseOver1 {
background: #F3F3FA;
color: #000000;
}
</style>
cell.onmouseout = function() {this.className='mouseOver1';};
cell.onmouseover = function() {this.className='mouseOut1';};
ÓÃÑùʽ±íÅäºÏ,javascript½øÐвÙ×÷,·Ç³£µÄÈÝÒ×!
......
function createXMLHttpRequest() { // ´´½¨XMLHTTPREQUESTµÄ±ê×¼º¯Êý.¼æÈݸ÷ä¯ÀÀÆ÷.
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
} ......
½â¾öAjax´«ËÍÖÐÎĻᵼÖÂÂÒÂëµÄÎÊÌâ
//Èç¹û´«ËͲÎÊýÊÇÖ±½Ó¸³ÓèµÄ£¬¾Í»á²úÉúÂÒÂë!
http_request.open("POST",url,true);
http_request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
http_request.send("action="+strName+"&val="+val);
//Èç¹ûvalµÄֵΪÖÐÎÄ£¬Ôò²úÉúÂÒÂë
//½â¾ö·½·¨ºÜ¼òµ¥£ºÊ¹ÓÃjavascriptÖеÄescape(string)
º¯Êý
http_request.open("POST",url,true);
http_request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
http_request.send("action="+strName+"&val="+escape(val));
//valµÄֵΪÖÐÎIJ»»á²úÉúÂÒÂë
encodeURI(encodeURI(post)); ÕâÑùÒ²¿ÉÒÔ
¹ØÓÚÔÚAjaxÖÐGET»ØµÄResponseTextÖÐÎÄÂÒÂëµÄ×î¼ò½â¾ö°ì·¨
ÓÃAjax
À´GET»ØÒ»¸öÒ³Ãæʱ£¬RESPONSETEXTÀïÃæµÄÖÐÎĶà°ë»á³öÏÖÂÒÂ룬ÕâÊÇÒòΪxmlhttpÔÚ´¦Àí·µ»ØµÄresponseTextµÄʱºò£¬ÊÇ°Ñ
resposeBody°´UTF-8±àÂë½ø½âÂ뿼Ðγɵģ¬Èç¹û·þÎñÆ÷ËͳöµÄȷʵÊÇUTF-8µÄÊý¾ÝÁ÷µÄʱºòºº×Ö»áÕýÈ·ÏÔʾ£¬¶øËͳöÁËGBK±àÂëÁ÷µÄʱºò¾ÍÂÒÁË¡£½â¾öµ ......
<html>
<head>
<title>jQuery Ajax ʵÀýÑÝʾ</title>
</head>
<script language="javascript" src="../lib/jquery.js"></script>
<script language="javascript">
$(document).ready(function ()
{
$('#send_ajax').click(function (){
var params=$('input').serialize(); //ÐòÁл¯±íµ¥µÄÖµ
$.ajax({
url:'ajax_json.php', //ºǫ́´¦Àí³ÌÐò
type:'post', //Êý¾Ý·¢ËÍ·½Ê½
dataType:'json', //½ÓÊÜÊý¾Ý¸ñʽ
data:params, //Òª´«µÝµÄÊý¾Ý
success:update_page //»Ø´«º¯Êý(ÕâÀïÊǺ¯ÊýÃû)
});
});
//$.post()·½Ê½£º
$('#test_post').click(function (){
&nb ......