1.1.1 ÕªÒª
Ajax¼¼ÊõµÄºËÐÄÊÇXMLHttpRequest¶ÔÏ󣨼ò³ÆXHR£©£¬¿ÉÒÔͨ¹ýʹÓÃXHR¶ÔÏó»ñÈ¡µ½·þÎñÆ÷µÄÊý¾Ý£¬È»ºóÔÙͨ¹ýDOM½«Êý¾Ý²åÈëµ½Ò³ÃæÖгÊÏÖ¡£ËäÈ»Ãû×ÖÖаüº¬XML£¬µ«AjaxͨѶÓëÊý¾Ý¸ñʽÎ޹أ¬ËùÒÔÎÒÃǵÄÊý¾Ý¸ñʽ¿ÉÒÔÊÇXML»òJSONµÈ¸ñʽ¡£
XMLHttpRequest¶ÔÏóÓÃÓÚÔÚºǫ́Óë·þÎñÆ÷½»»»Êý¾Ý£¬¾ßÌå×÷ÓÃÈçÏ£º
±¾ÎÄĿ¼ 1.1.2 ÕýÎÄXMLHttpRequestÊÇÒ»¸öJavaScript¶ÔÏó£¬ËüÊÇÓÉ΢ÈíÉè¼Æ£¬²¢ÇÒ±»Mozilla¡¢AppleºÍGoogle²ÉÄÉ£¬W3CÕýÔÚ±ê×¼»¯Ëü¡£ËüÌṩÁËÒ»ÖÖ¼òµ¥µÄ·½·¨À´¼ìË÷URLÖеÄÊý¾Ý¡£
ÎÒÃÇÒª´´½¨Ò»¸öXMLHttpRequestʵÀý£¬Ö»ÐènewÒ»¸ö¾ÍOKÁË£º
//// Creates a XMLHttpRequest object. var req = new XMLHttpRequest();Ò²ÐíÓÐÈË»á˵£º¡°Õâ¿É²»Ðа¡£¡IE6²»Ö§³ÖÔʼµÄXHR¶ÔÏó¡±£¬È·ÊµÊÇÕâÑù£¬ÎÒÃÇÔÚºóÃ潫»á½éÉÜÖ§³ÖIE6»ò¸üÀÏ°æ±¾´´½¨XHR¶ÔÏóµÄ·½·¨¡£
XMLHttpRequestµÄÓ÷¨ÔÚ´´½¨XHR¶ÔÏóºó£¬½Ó×ÅÎÒÃÇÒªµ÷ÓÃÒ»¸ö³õʼ»¯·½·¨open()£¬Ëü½ÓÊÜÎå¸ö²ÎÊý¾ßÌ嶨ÒåÈçÏ£º
void open( DOMString method, //"GET", "POST", "PUT", "DELETE" DOMString url, optional boolean async, optional DOMString user, optional DOMString password );ͨ¹ýÉÏÃæµÄ¶¨ÒåÎÒÃÇÖªµÀopen()·½·¨µÄÇ©Ãû°üº¬Îå¸ö²ÎÊý£¬ÆäÖÐÓвÎÊýmethodºÍurlµØÖ·ÊDZØÌîµÄ£¬¼ÙÉèÎÒÃÇÕë¶ÔURL: myxhrtest.aspx·¢ËÍGETÇëÇó»ñÈ¡Êý¾Ý£¬¾ßÌ嶨ÒåÈçÏ£º
var req = new XMLHttpRequest(); req.open( "GET", "myxhrtest.aspx", false );ͨ¹ýÉÏÊö´úÂë»áÆô¶¯Ò»¸öÕë¶Ômyxhrtest.aspxµÄGETÇëÇó£¬ÕâÀïÓÐÁ½µãҪעÒ⣺һÊÇURLÏà¶ÔÓÚÖ´ÐдúÂëµÄµ±Ç°Ò³Ã棨ʹÓþø¶Ô·¾¶£©£»¶þÊǵ÷ÓÃopen()·½·¨²¢²»»áÕæÕý·¢ËÍÇëÇ󣬶øÖ»ÊÇÆô¶¯Ò»¸öÇëÇó×¼±¸·¢ËÍ¡£
Ö»ÄÜÏòͬһ¸öÓòÖÐʹÓÃÏàͬ¶Ë¿ÚºÍÐÒéµÄURLÖз¢ËÍÇëÇó£»Èç¹ûURLÓëÆô¶¯ÇëÇóµÄÒ³ÃæÓÐÈκβî±ð£¬¶¼»áÒý·¢°²È«´íÎó¡£
ÒªÕæÕý·¢ËÍÇëÇóҪʹÓÃsend()·½·¨£¬send()·½·¨½ÓÊÜÒ»¸ö²ÎÊý£¬¼´Òª×÷ΪÇëÇóÖ÷Ìå·¢Ë͵ÄÊý¾Ý£¬Èç¹û²»ÐèҪͨ¹ýÇëÇóÖ÷Ìå·¢ËÍÊý¾Ý£¬ÎÒÃDZØÐë´«µÝÒ»¸önullÖµ¡£ÔÚµ÷ÓÃsend()Ö®ºó£¬ÇëÇó¾Í»á±»·ÖÅɵ½·þÎñÆ÷£¬ÍêÕûAjaxÇëÇó´úÂëÈçÏ£º
var req = new XMLHttpRequest(); req.open( "GET", "myxhrtest.aspx", false ); req.send(null);ÔÚ·¢ËÍÇëÇóÖ®ºó£¬ÎÒÃÇÐèÒª¼ì²éÇëÇóÊÇ·ñÖ´Ðгɹ¦£¬Ê×ÏÈ¿ÉÒÔͨ¹ýstatusÊôÐÔÅжϣ¬Ò»°ãÀ´Ëµ£¬¿ÉÒÔ½«HTTP״̬´úÂëΪ200×÷Ϊ³É¹¦±êÖ¾¡£Õâʱ£¬ÏìÓ¦Ö÷ÌåÄÚÈݻᱣ´æµ½responseTextÖС£´ËÍ⣬״̬´úÂëΪ304±íʾÇëÇóµÄ×ÊÔ´²¢Ã»Óб»Ð޸ģ¬¿ÉÒÔÖ±½ÓʹÓÃä¯ÀÀÆ÷»º´æµÄÊý¾Ý£¬AjaxµÄͬ²½ÇëÇó´úÂëÈçÏ£º
if (req != null) { req.onreadystatechange = function() { if ((req.status >= 200 && req.status < 300) || req.status == 304) { //// Do something. } else { alert("Request was unsuccessful: " + req.status); } }; req.open("GET", "", true); req.send(null); }Ç°ÃæÎÒÃǶ¨ÒåÁËAjaxµÄͬ²½ÇëÇó£¬Èç¹ûÎÒÃÇ·¢ËÍÒì²½ÇëÇó£¬ÄÇôÔÚÇëÇó¹ý³ÌÖÐjavascript´úÂë»á¼ÌÐøÖ´ÐУ¬Õâʱ¿ÉÒÔͨ¹ýreadyStateÊôÐÔÅжÏÇëÇóµÄ״̬£¬µ±readyState = 4ʱ£¬±íʾÊÕµ½È«²¿ÏìÓ¦Êý¾Ý£¬ÊôÐÔÖµµÄ¶¨ÒåÈçÏ£º
readyStateÖµ
ÃèÊö
0
δ³õʼ»¯£»ÉÐδµ÷ÓÃopen()·½·¨
1
Æô¶¯£»ÉÐδµ÷ÓÃsend()·½·¨
2
ÒÑ·¢ËÍ£»µ«ÉÐδÊÕµ½ÏìÓ¦
3
½ÓÊÕ£»ÒѾÊÕµ½²¿·ÖÏìÓ¦Êý¾Ý
4
Íê³É£»ÊÕµ½È«²¿ÏìÓ¦Êý¾Ý
±í1 readyStateÊôÐÔÖµ
ͬ²½ÇëÇ󣺷¢ÉúÇëÇóºó£¬ÒªµÈ´ý·þÎñÆ÷Ö´ÐÐÍê±Ï²Å¼ÌÐøÖ´Ðе±Ç°´úÂë¡£
Òì²½ÇëÇ󣺷¢ÉúÇëÇóºó£¬ÎÞÐèµÈµ½·þÎñÆ÷Ö´ÐÐÍê±Ï£¬¿ÉÒÔ¼ÌÐøÖ´Ðе±Ç°´úÂë¡£
ÏÖÔÚÎÒÃÇÒªÔö¼ÓÅжÏreadyStateÊôÐÔÖµ£¬µ±readyState = 4ʱ£¬±íʾȫ²¿Êý¾Ý½ÓÊÕÍê³É£¬ ËùÒÔAjaxµÄÒì²½ÇëÇó´úÂëÈçÏ£º
if (req != null) { req.onreadystatechange = function() { //// Checks the asyn request completed or not. if (req.readyState == 4) { if ((req.status >= 200 && req.status < 300) || req.status == 304) { //// Do something. } else { alert("Request was unsuccessful: " + req.status); } } }; req.open("GET", "", true); req.send(null); } AjaxͬԴÇëÇóÏÖÔÚÎÒÃǶÔAjaxµÄÇëÇóʵÏÖÓÐÁ˳õ²½µÄÁ˽⣬½ÓÏÂÀ´ÎÒÃǽ«Í¨¹ý¾ßÌåµÄÀý×Ó˵Ã÷AjaxÇëÇóµÄÓ¦Óó¡ºÏºÍ¾ÖÏÞ¡£
ÔÚÈÕ³£ÍøÂçÉú»îÖУ¬ÎÒÃÇÔÚä¯ÀÀÆ÷µÄµØÖ·ÖÐÊäÈëÒª·ÃÎʵÄURL²¢Çһسµ£¬ä¯ÀÀÆ÷»áÏò·þÎñÆ÷·¢ËÍÇëÇ󣬵±·þÎñÆ÷ÊÕµ½ÇëÇóºó£¬°ÑÏàÓ¦µÄÇëÇóÒ³Ãæ·¢ËÍ»Øä¯ÀÀÆ÷£¬ÎÒÃǻᷢÏÖÒ³Ãæ´ó²¿·Ö¼ÓÔØÍê±Ï£¬ÓÐЩ»¹Ã»ÓмÓÔØÍê±Ï¡£×ܵÃÀ´Ëµ£¬²ÉÓÃÒì²½¼ÓÔØ·½Ê½²»»áÓ°ÏìÒѼÓÔØÍê±ÏµÄÒ³Ãæä¯ÀÀ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýAjaxʵÏÖÒì²½¼ÓÔØ¡£
ÕâÀïÎÒÃÇÒÔAdventureWorksÊý¾Ý¿âΪÀý£¬°Ñ²úÆ·±í£¨Product£©ÖеÄÊý¾Ýͨ¹ý±¨±í³ÊÏÖ¸øÓû§£¬ÎÒÃÇ¿ÉÒÔͨ¹ý¶àÖÖ·½·¨ÊµÏָñ¨±íÐèÇó£¬ÕâÀïÎÒÃǽ«Í¨¹ýAjaxʵÏָù¦ÄÜ¡£
Ê×ÏÈ£¬ÎÒÃÇÒª°Ñºǫ́Êý¾Ýת»»ÎªJSON¸ñʽ£¬½ÓÏÂÀ´ÎÒÃǶ¨ÒåProduct±íµÄÊý¾Ý¿â·ÃÎʶÔÏó£¨DAO£©£¬¾ßÌåµÄʵÏÖ´úÂëÈçÏ£º
/// <summary> /// The product datatable dao. /// </summary> public class ProductDao { /// <summary> /// Initializes a new instance of the <see cref="ProductDao"/> class. /// </summary> public ProductDao() { } /// <summary> /// Gets or sets the product id. /// </summary> public int Id { get; set; } /// <summary> /// Gets or sets the product name. /// </summary> public string Name { get; set; } /// <summary> /// Gets or sets the product serial number. /// </summary> public string SerialNumber { get; set; } /// <summary> /// Gets or sets the product qty. /// </summary> public short Qty { get; set; } }Ç°ÃæÎÒÃǶ¨ÒåÁËProduct±íµÄÊý¾Ý¿â·ÃÎʶÔÏ󡪡ªProductDao£¬Ëü°üº¬ËĸöÊôÐÔ·Ö±ðÊDzúÆ·µÄId£¬Ãû³Æ£¬ÐòÁкźÍÏúÊÛÊýÁ¿¡£
¡¡