Ïêϸ½â¶ÁJquery¸÷Ajaxº¯Êý£º
$.get(),$.post(),$.ajax(),$.getJSON()
Ò»£¬$.get(url,[data],[callback])
˵Ã÷£ºurlΪÇëÇóµØÖ·£¬dataΪÇëÇóÊý¾ÝµÄÁÐ±í£¬callbackΪÇëÇó³É¹¦ºóµÄ»Øµ÷º¯Êý£¬¸Ãº¯Êý½ÓÊÜÁ½¸ö²ÎÊý£¬µÚÒ»¸öΪ·þÎñÆ÷·µ»ØµÄÊý¾Ý£¬µÚ¶þ¸ö²ÎÊýΪ·þÎñÆ÷µÄ״̬£¬ÊÇ¿ÉÑ¡²ÎÊý¡£
¶øÆäÖУ¬·þÎñÆ÷·µ»ØÊý¾ÝµÄ¸ñʽÆäʵÊÇ×Ö·û´®ÐÎÊÆ£¬²¢²»ÊÇÎÒÃÇÏëÒªµÄjsonÊý¾Ý¸ñʽ£¬ÔÚ´ËÒýÓÃÖ»ÊÇΪÁ˶ԱÈ˵Ã÷
$.get("data.php",$("#firstName.val()"),function(data){ $("#getResponse").html(data); }//·µ»ØµÄdataÊÇ×Ö·û´®ÀàÐÍ );
¶þ£¬$.post(url,[data],[callback],[type])
˵Ã÷£ºÕâ¸öº¯Êý¸ú$.get()²ÎÊý²î²»¶à£¬¶àÁËÒ»¸ötype²ÎÊý£¬typeΪÇëÇó µÄÊý¾ÝÀàÐÍ£¬¿ÉÒÔÊÇhtml,xml,jsonµÈÀàÐÍ£¬Èç¹ûÎÒÃÇÉèÖÃÕâ¸ö²ÎÊýΪ£ºjson£¬ÄÇô·µ»ØµÄ¸ñʽÔòÊÇjson¸ñʽµÄ£¬Èç¹ûûÓÐÉèÖã¬¾Í ºÍ$.get()·µ»ØµÄ¸ñʽһÑù£¬¶¼ÊÇ×Ö·û´®µÄ
$.post("data.php",$("#firstName.val()"),function(data){ $("#postResponse").html(data.name); },"json"//ÉèÖÃÁË»ñÈ¡Êý¾ÝµÄÀàÐÍ£¬ËùÒԵõ½µÄÊý¾Ý¸ñʽΪjsonÀàÐ굀 );
Èý£¬$.ajax(opiton)
˵Ã÷£º$.ajax()Õâ¸öº¯Êý¹¦ÄÜÇ¿´ó£¬¿ÉÒÔ¶Ôajax½øÐÐÐí¶à¾«È·µÄ¿ØÖÆ£¬ÐèÒªÏêϸ˵Ã÷µÄÇë²ÎÕÕÏà¹Ø×ÊÁÏ
$.ajax({ url: "ajax/ajax_selectPicType.aspx", data:{Full:"fu"}, type: "POST", dataType:'json', success:CallBack, error:function(er){ BackErr(er);} });
ËÄ£¬$.getJSON(url,[data],[callback])
$.getJSON("data.php",$("#firstName.val()"),function(jsonData){ $("#getJSONResponse").html(jsonData.id);}//ÎÞÐèÉèÖã¬Ö±½Ó»ñÈ¡µÄÊý¾ÝÀàÐÍΪjson£¬ ËùÒÔµ÷ÓÃʱÐèҪʹÓÃjsonData.id·½Ê½ );
When Ajax meets jQuery »ùÓÚAJAXµÄÓ¦ÓÃÏÖÔÚÔ½À´Ô½¶à£¬¶ø¶ÔÓÚǰ̨¿ª·¢ÈËÔ±À´Ëµ£¬Ö±½ÓºÍµ×²ãµÄHTTPRequest´ò½»µÀÓÖ²»ÊÇÒ»¼þÁîÈËÓä¿ìµÄÊÂÇé¡£jQuery¼ÈÈ»·â×°ÁË JavaScript£¬¿Ï¶¨ÒѾ¿¼ÂǹýAJAXÓ¦ÓõÄÎÊÌâ¡£µÄÈ·£¬Èç¹ûÓÃjQueryдAJAX»á±ÈÖ±½ÓÓÃJSд·½±ãN±¶¡££¨²»ÖªµÀÓÃjQuery³¤ÁË£¬ »á²»»áɥʧ¶ÔJSµÄ֪ʶÁË¡¡£© ÕâÀï¼ÙÉè´ó¼Ò¶ÔjQueryÓï·¨ÒѾ±È½ÏÊìϤ£¬À´¶ÔajaxµÄһЩӦÓÃ×÷һЩ×ܽᡣ
ÔØÈ뾲̬ҳÃæ
- load( url, [data], [callback] );
- url (String) ÇëÇóµÄHTMLÒ³µÄURLµØÖ·
data (Map)(¿ÉÑ¡²ÎÊý) ·¢ËÍÖÁ·þÎñÆ÷µÄ key/value Êý¾Ý
callback (Callback) (¿ÉÑ¡²ÎÊý) ÇëÇóÍê³Éʱ(²»ÐèÒªÊÇsuccessµÄ)µÄ»Øµ÷º¯Êý
load()·½·¨¿ÉÒÔÇáËÉÔØÈ뾲̬ҳÃæÄÚÈݵ½Ö¸¶¨jQuery¶ÔÏó¡£$('#ajax-div').load('data.html');
ÕâÑù£¬data.htmlµÄÄÚÈݽ«±»ÔØÈëµ½IDΪajax-divµÄDOM¶ÔÏóÖ®ÄÚ¡£ÄãÉõÖÁ¿ÉÒÔͨ¹ýÖƶ¨IDÀ´ÊµÏÖÔØÈ벿·ÖÄÚÈݵÄAjax²Ù×÷£¬È磺
$('#ajax-div').load('data.html#my-section');
ʵÏÖGETºÍPOST·½·¨
- get( url, [data], [callback] )
- url (String) ·¢ËÍÇëÇóµÄURLµØÖ·.
data (Map)(¿ÉÑ¡²ÎÊý) Òª·¢Ë͸ø·þÎñÆ÷µÄÊý¾Ý£¬ÒÔ Key/value µÄ¼üÖµ¶ÔÐÎʽ±íʾ£¬»á×öΪQueryString¸½¼Óµ½ÇëÇóURLÖÐ
callback (Callback) (¿ÉÑ¡²ÎÊý) ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý(Ö»Óе±ResponseµÄ·µ»Ø״̬ÊÇsuccess²ÅÊǵ÷Óø÷½·¨)
ºÜÏÔÈ»ÕâÊÇÒ»¸öרÃÅʵÏÖGET·½Ê½µÄº¯Êý£¬Ê¹ÓÃÆðÀ´Ò²Ï൱µÄ¼òµ¥$.get('login.php', { id : 'Robin', password: '123456', gate : 'index' }, function(data, status) { //dataΪ·µ»Ø¶ÔÏó£¬statusΪÇëÇóµÄ״̬ alert(data); //´Ëʱ¼ÙÉè·þÎñÆ÷½Å±¾»á·µ»ØÒ»¶ÎÎÄ×Ö"ÄãºÃ£¬Robin£¡"£¬ ÄÇôä¯ÀÀÆ÷¾Í»áµ¯³ö¶Ô»°¿òÏÔʾ¸Ã¶ÎÎÄ×Ö alert(status); //½á¹ûΪsuccess, errorµÈµÈ£¬µ«ÕâÀïÊdzɹ¦Ê±²ÅÄÜÔËÐеĺ¯Êý });
- post( url, [data], [callback], [type] )
- url (String) ·¢ËÍÇëÇóµÄURLµØÖ·.
data (Map)(¿ÉÑ¡²ÎÊý) Òª·¢Ë͸ø·þÎñÆ÷µÄÊý¾Ý£¬ÒÔ Key/value µÄ¼üÖµ¶ÔÐÎʽ±íʾ
callback (Callback) (¿ÉÑ¡²ÎÊý) ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý(Ö»Óе±ResponseµÄ·µ»Ø״̬ÊÇsuccess²ÅÊǵ÷Óø÷½·¨)
type (String) (¿ÉÑ¡²ÎÊý) ÇëÇóÊý¾ÝµÄÀàÐÍ£¬xml,text,jsonµÈ
ͬÑùÊÇjQueryÌṩµÄÒ»¸ö¼ò±ãº¯Êý£¬ÆäʵÓ÷¨$.post('regsiter.php', { id:'Robin', password: '123456', type:'user' },function(data, status) { alert(data); }, "json");
ʼþÇý¶¯µÄ½Å±¾ÔØÈ뺯Êý£ºgetScript()
- getScript( url, [callback] )
- url (String) ´ýÔØÈë JS ÎļþµØÖ·
callback (Function) (¿ÉÑ¡) ³É¹¦ÔØÈëºó»Øµ÷º¯Êý
getScript()º¯Êý¿ÉÒÔÔ¶³ÌÔØÈëJavaScript½Å±¾²¢ÇÒÖ´ÐС£Õâ¸öº¯Êý¿ÉÒÔ¿ç ÓòÔØÈëJSÎļþ£¨ÉñÆæ¡¡£¿£¡£©¡£Õâ¸öº¯ÊýµÄÒâÒåÊǾ޴ó µÄ£¬Ëü¿ÉÒԺܴó³Ì¶ÈµÄËõ¼õÒ³Ãæ³õ´ÎÔØÈëµÄ´úÂëÁ¿£¬ÒòΪÄã¿ÉÒÔ¸ù¾ÝÓû§µÄ½»»¥À´ÔØÈëÏàÓ¦µÄJSÎļþ£¬¶ø²»±ØÔÚÒ³Ãæ³õʼ»¯µÄʱºòÈ«²¿ÔØÈë¡£$.getScript('ajaxEvent.js', function() { alert("Scripts Loaded!"); //ÔØÈëajaxEvent.js£¬²¢ÇÒÔڳɹ¦ÔØÈëºóÏÔʾ¶Ô»°¿òÌáʾ¡£ });
¹¹½¨Êý¾ÝͨѶµÄÇÅÁº£ºgetJSON()
- getJSON(url,[data],[callback])
- url (String) ·¢ËÍÇëÇóµØÖ·
data (Map) (¿ÉÑ¡) ´ý·¢ËÍ Key/value ²ÎÊý
callback (Function) (¿ÉÑ¡) ÔØÈë³É¹¦Ê±»Øµ÷º¯Êý¡£
JSONÊÇÒ»ÖÖÀíÏëµÄÊý¾Ý´«Êä¸ñʽ£¬ËüÄܹ»ºÜºÃµÄÈÚºÏÓëJavaScript»òÆäËûËÞÖ÷Óï ÑÔ£¬²¢ÇÒ¿ÉÒÔ±»JSÖ±½ÓʹÓá£Ê¹ÓÃJSONÏà±È´«Í³µÄͨ¹ý GET¡¢POSTÖ±½Ó·¢ËÍ¡±ÂãÌ塱Êý¾Ý£¬ÔڽṹÉϸüΪºÏÀí£¬Ò²¸üΪ°²È«¡£ÖÁÓÚjQueryµÄgetJSON()º¯Êý£¬Ö»ÊÇÉèÖÃÁËJSON²ÎÊýµÄ ajax()º¯ÊýµÄÒ»¸ö¼ò»¯°æ±¾¡£Õâ¸öº¯ÊýÒ²ÊÇ¿ÉÒÔ¿çÓòʹÓõģ¬Ïà±Èget()¡¢post()ÓÐÒ»¶¨ÓÅÊÆ¡£ÁíÍâÕâ¸öº¯Êý¿ÉÒÔͨ¹ý°ÑÇëÇóurlд ³É¡±myurl?callback=X¡±ÕâÖÖ¸ñʽ£¬ÈóÌÐòÖ´Ðлص÷º¯ÊýX¡£$.getJSON('feed.php',{ request: images, id: 001, size: large }, function(json) { alert(json.images[0].link); //´Ë´¦json¾ÍÊÇÔ¶³Ì´«»ØµÄjson¶ÔÏ󣬼ÙÉèÆä¸ñʽÈçÏ£º //{'images' : [ // {link: images/001.jpg, x :100, y : 100}, // {link: images/002.jpg, x : 200, y 200:} //]}; } );
¸üµ×²ãµÄajax()º¯Êý
ËäÈ»get()ºÍpost()º¯Êý·Ç³£¼ò½àÒ×Ó㬵«ÊǶÔÓÚ¸ü¸´ÔÓµÄһЩÉè¼ÆÐèÇó»¹ÊÇÎÞ·¨ÊµÏÖ£¬±ÈÈçÔÚajax·¢Ë͵IJ»Í¬Ê±¶Î×ö³ö²»Í¬µÄ¶¯×÷µÈ¡£jQueryÌṩһ¸ö¸üΪ¾ßÌåµÄº¯Êý£ºajax()¡£
- ajax( options )
- ajax()ÌṩÁËÒ»´óƱ²ÎÊý£¬ËùÒÔ¿ÉÒÔʵÏÖÏ൱¸´ÔӵŦÄÜ¡£
²ÎÊýÃû ÀàÐÍ ÃèÊö url String (ĬÈÏ: µ±Ç°Ò³µØÖ·) ·¢ËÍÇëÇóµÄµØÖ·¡£ type String (ĬÈÏ: ¡°GET¡±) ÇëÇó·½Ê½ (¡°POST¡± »ò ¡°GET¡±)£¬ ĬÈÏΪ ¡°GET¡±¡£
×¢Ò⣺ÆäËü HTTP ÇëÇó·½·¨£¬Èç PUT ºÍ DELETE Ò²¿ÉÒÔʹÓ㬵«½ö²¿·Öä¯ÀÀÆ÷Ö§³Ö¡£timeout Number ÉèÖÃÇëÇó³¬Ê±Ê±¼ä£¨ºÁÃ룩¡£´ËÉèÖý«¸²¸ÇÈ«¾ÖÉèÖᣠasync Boolean (ĬÈÏ: true) ĬÈÏÉèÖÃÏ£¬ËùÓÐÇëÇó¾ùΪÒì²½ÇëÇó¡£
Èç¹ûÐèÒª·¢ËÍͬ²½ÇëÇó£¬Ç뽫´ËÑ¡ÏîÉèÖÃΪ false¡£
×¢Ò⣬ͬ²½ÇëÇó½«Ëøסä¯ÀÀÆ÷£¬Óû§ÆäËü²Ù×÷±ØÐëµÈ´ýÇëÇóÍê³É²Å¿ÉÒÔÖ´ÐС£beforeSend Function ·¢ËÍÇëÇóÇ°¿ÉÐÞ¸Ä XMLHttpRequest ¶ÔÏóµÄº¯Êý£¬ÈçÌí¼Ó×Ô¶¨Òå HTTP Í·¡£
XMLHttpRequest ¶ÔÏóÊÇΨһµÄ²ÎÊý¡£function (XMLHttpRequest) { this; // the options for this ajax request }
cache Boolean (ĬÈÏ: true) jQuery 1.2 й¦ÄÜ£¬ÉèÖÃΪ false ½«²»»á´Óä¯ÀÀÆ÷»º´æÖмÓÔØÇëÇóÐÅÏ¢¡£ complete Function ÇëÇóÍê³Éºó»Øµ÷º¯Êý (ÇëÇó³É¹¦»òʧ°Üʱ¾ùµ÷ÓÃ)¡£
²ÎÊý£º XMLHttpRequest ¶ÔÏ󣬳ɹ¦ÐÅÏ¢×Ö·û´®¡£function (XMLHttpRequest, textStatus) { this; // the options for this ajax request }
contentType String (ĬÈÏ: ¡°application/x-www-form-urlencoded¡±) ·¢ËÍÐÅÏ¢ÖÁ·þÎñÆ÷ʱÄÚÈݱàÂëÀàÐÍ¡£Ä¬ÈÏÖµÊʺϴó¶àÊýÓ¦Óó¡ºÏ¡£ data Object,
String·¢Ë͵½·þÎñÆ÷µÄÊý¾Ý¡£½«×Ô¶¯×ª»»ÎªÇëÇó×Ö·û´®¸ñʽ¡£GET ÇëÇóÖн«¸½¼ÓÔÚ URL ºó¡£
²é¿´ processData Ñ¡Ïî˵Ã÷ÒÔ½ûÖ¹´Ë×Ô¶¯×ª»»¡£±ØÐëΪ Key/Value ¸ñʽ¡£
Èç¹ûΪÊý×飬jQuery ½«×Ô¶¯Îª²»Í¬Öµ¶ÔӦͬһ¸öÃû³Æ¡£
Èç {foo:["bar1", "bar2"]} ת»»Îª ‘&foo=bar1&foo=bar2′¡£dataType String Ô¤ÆÚ·þÎñÆ÷·µ»ØµÄÊý¾ÝÀàÐÍ¡£Èç¹û²»Ö¸¶¨£¬jQuery ½«×Ô¶¯¸ù¾Ý HTTP °ü MIME ÐÅÏ¢
·µ»Ø responseXML »ò responseText£¬²¢×÷Ϊ»Øµ÷º¯Êý²ÎÊý´«µÝ£¬¿ÉÓÃÖµ:¡°xml¡±: ·µ»Ø XML Îĵµ£¬¿ÉÓà jQuery ´¦Àí¡£
¡°html¡±: ·µ»Ø´¿Îı¾ HTML ÐÅÏ¢£»°üº¬ script ÔªËØ¡£
¡°script¡±: ·µ»Ø´¿Îı¾ JavaScript ´úÂë¡£²»»á×Ô¶¯»º´æ½á¹û¡£
¡°json¡±: ·µ»Ø JSON Êý¾Ý ¡£
¡°jsonp¡±: JSONP ¸ñʽ¡£Ê¹Óà JSONP ÐÎʽµ÷Óú¯Êýʱ£¬
Èç ¡°myurl?callback=?¡± jQuery ½«×Ô¶¯Ìæ»» ? ΪÕýÈ·µÄº¯ÊýÃû£¬ÒÔÖ´Ðлص÷º¯Êý¡£
error Function (ĬÈÏ: ×Ô¶¯ÅÐ¶Ï (xml »ò html)) ÇëÇóʧ°Üʱ½«µ÷Óô˷½·¨¡£ Õâ¸ö·½·¨ÓÐÈý¸ö²ÎÊý£ºXMLHttpRequest ¶ÔÏ󣬴íÎóÐÅÏ¢£¬£¨¿ÉÄÜ£©²¶»ñµÄ´íÎó¶ÔÏó¡£
function (XMLHttpRequest, textStatus, errorThrown) { // ͨ³£Çé¿öÏÂtextStatusºÍerrorThownÖ»ÓÐÆäÖÐÒ»¸öÓÐÖµ this; // the options for this ajax request }
global Boolean (ĬÈÏ: true) ÊÇ·ñ´¥·¢È«¾Ö AJAX ʼþ¡£ÉèÖÃΪ false ½«²»»á´¥·¢È«¾Ö AJAX ʼþ£¬ Èç ajaxStart »ò ajaxStop ¡£¿ÉÓÃÓÚ¿ØÖƲ»Í¬µÄAjaxʼþ
ifModified Boolean (ĬÈÏ: false) ½öÔÚ·þÎñÆ÷Êý¾Ý¸Ä±äʱ»ñÈ¡ÐÂÊý¾Ý¡£ ʹÓà HTTP °ü Last-Modified Í·ÐÅÏ¢Åжϡ£
processData Boolean (ĬÈÏ: true) ĬÈÏÇé¿öÏ£¬·¢Ë͵ÄÊý¾Ý½«±»×ª»»Îª¶ÔÏó(¼¼ÊõÉϽ²²¢·Ç×Ö·û´®) ÒÔÅäºÏĬÈÏÄÚÈÝÀàÐÍ ¡°application/x-www-form-urlencoded¡±¡£
Èç¹ûÒª·¢ËÍ DOM Ê÷ÐÅÏ¢»òÆäËü²»Ï£Íûת»»µÄÐÅÏ¢£¬ÇëÉèÖÃΪ false¡£
success Function ÇëÇó³É¹¦ºó»Øµ÷º¯Êý¡£Õâ¸ö·½·¨ÓÐÁ½¸ö²ÎÊý£º·þÎñÆ÷·µ»ØÊý¾Ý£¬·µ»Ø״̬ function (data, textStatus) { // data could be xmlDoc, jsonObj, html, text, etc... this; // the options for this ajax request }
Äã ¿ÉÒÔÖ¸¶¨xml¡¢script¡¢html¡¢json×÷ΪÆäÊý¾ÝÀàÐÍ£¬¿ÉÒÔΪbeforeSend¡¢error¡¢sucess¡¢completeµÈ״̬ÉèÖà ´¦Àíº¯Êý£¬ÖÚ¶àÆäËü²ÎÊýÒ²¿ÉÒÔ¶©ÍêÍêÈ«È«¶¨ÒåÓû§µÄAjaxÌåÑé¡£ÏÂÃæµÄÀý×ÓÖУ¬ÎÒÃÇÓÃajax()À´µ÷ÓÃÒ»¸öXMLÎĵµ£º
$.ajax({ url: 'doc.xml', type: 'GET', dataType: 'xml', timeout: 1000, error: function(){ alert('Error loading XML document'); }, success: function(xml){ alert(xml); //´Ë´¦xml¾ÍÊÇXMLµÄjQuery¶ÔÏóÁË£¬Äã¿ÉÒÔÓÃfind()¡¢next()»òXPathµÈ·½·¨ÔÚÀïÃæÑ°Õҽڵ㣬 ºÍÓÃjQuery²Ù×÷HTML¶ÔÏóûÓÐÇø±ð } });
½øÒ»²½Á˽âAJAXʼþ
Ç°ÃæÌÖÂÛµÄһЩ·½·¨¶¼ÓÐ×Ô¼ºµÄʼþ´¦Àí»úÖÆ£¬´ÓÒ³ÃæÕûÌåÀ´Ëµ£¬¶¼Ö»ÄÜ˵ÊǾֲ¿º¯Êý¡£jQueryÌṩÁËAJAXÈ«¾Öº¯ÊýµÄ¶¨Ò壬ÒÔÂú×ãÌØÊâµÄÐèÇó¡£ÏÂÃæÊÇjQueryÌṩµÄËùÓк¯Êý£¨°´ÕÕ´¥·¢Ë³ÐòÅÅÁÐÈçÏ£©£º
- ajaxStart
- (È«¾Öʼþ) ¿ªÊ¼ÐµÄAjaxÇëÇ󣬲¢ÇÒ´ËʱûÓÐÆäËûajaxÇëÇóÕýÔÚ½øÐÐ
- beforeSend
- (¾Ö²¿Ê¼þ) µ±Ò»¸öAjaxÇëÇó¿ªÊ¼Ê±´¥·¢¡£Èç¹ûÐèÒª£¬Äã¿ÉÒÔÔÚÕâÀïÉèÖÃXMLHttpRequest¶ÔÏó
- ajaxSend
- (È«¾Öʼþ) ÇëÇó¿ªÊ¼Ç°´¥·¢µÄÈ«¾Öʼþ
- success
- (¾Ö²¿Ê¼þ) ÇëÇó³É¹¦Ê±´¥·¢¡£¼´·þÎñÆ÷ûÓзµ»Ø´íÎ󣬷µ»ØµÄÊý¾ÝҲûÓдíÎó
- ajaxSuccess
- È«¾ÖʼþÈ«¾ÖµÄÇëÇó³É¹¦
- error
- (¾Ö²¿Ê¼þ) ½öµ±·¢Éú´íÎóʱ´¥·¢¡£ÄãÎÞ·¨Í¬Ê±Ö´ÐÐsuccessºÍerrorÁ½¸ö»Øµ÷º¯Êý
- ajaxError
- È«¾ÖʼþÈ«¾ÖµÄ·¢Éú´íÎóʱ´¥·¢
- complete
- (¾Ö²¿Ê¼þ) ²»¹ÜÄãÇëÇó³É¹¦»¹ÊÇʧ°Ü£¬¼´±ãÊÇͬ²½ÇëÇó£¬Ä㶼ÄÜÔÚÇëÇóÍê³Éʱ´¥·¢Õâ¸öʼþ
- ajaxComplete
- È«¾ÖʼþÈ«¾ÖµÄÇëÇóÍê³Éʱ´¥·¢
- ajaxStop
- (È«¾Öʼþ) µ±Ã»ÓÐAjaxÕýÔÚ½øÐÐÖеÄʱºò£¬´¥·¢
¾Ö²¿Ê¼þÔÚ֮ǰµÄº¯ÊýÖж¼ÓнéÉÜ£¬ÎÒÃÇÖ÷ÒªÀ´¿´¿´È«¾Öʼþ¡£¶Ôij¸ö¶ÔÏó½øÐÐÈ«¾Öʼþ¼àÌý£¬ÄÇôȫ¾ÖÖеÄAJAX¶¯×÷£¬¶¼»á¶ÔÆä²úÉúÓ°Ïì¡£±ÈÈ磬µ±Ò³ÃæÔÚ½øÐÐAJAX²Ù×÷ʱ£¬IDΪ¡±loading¡±µÄDIV¾ÍÏÔʾ³öÀ´£º
$("#loading").ajaxStart(function(){ $(this).show(); });
È«¾ÖʼþÒ²¿ÉÒÔ°ïÖúÄã±àдȫ¾ÖµÄ´íÎóÏàÓ¦ºÍ³É¹¦ÏàÓ¦£¬¶ø²»ÐèҪΪÿ¸öAJAXÇëÇó¶ÀÁ¢ÉèÖá£ÓбØÒªÖ¸³ö£¬È«¾ÖʼþµÄ²ÎÊýÊǺÜÓÐÓõġ£³ýÁË ajaxStart¡¢ajaxOptions£¬ÆäËûʼþ¾ùÓÐevent, XMLHttpRequest, ajaxOptionsÈý¸ö²ÎÊý¡£µÚÒ»¸ö²ÎÊý¼´Ê¼þ±¾Éí£»µÚ¶þ¸öÊÇXHR¶ÔÏó£»µÚÈý¸öÊÇÄã´«µÝµÄajax²ÎÊý¶ÔÏó¡£ÔÚÒ»¸ö¶ÔÏóÀïÏÔʾȫ¾ÖµÄAJAXÇé¿ö£º
$("#msg").beforeSend(function(e,xhr,o) { $(this).html("ÕýÔÚÇëÇó"+o.url); }).ajaxSuccess(function(e,xhr,o) { $(this).html(o.url+"ÇëÇó³É¹¦"); }).ajaxError(function(e,xhr,o) { $(this).html(o.url+"ÇëÇóʧ°Ü"); });
ºÜÏÔÈ»£¬µÚÈý¸ö²ÎÊýÒ²¿ÉÒÔ°ïÖúÄã´«µÝÄãÔÚAJAXʼþÀï¼ÓÈëµÄ×Ô¶¨Òå²ÎÊý¡£ ÔÚµ¥¸öAJAXÇëÇóʱ£¬Äã¿ÉÒÔ½«globalµÄÖµÉèΪfalse£¬ÒÔ½«´ËÇëÇó¶ÀÁ¢ÓÚAJAXµÄÈ«¾Öʼþ¡£
$.ajax({ url: "request.php", global: false, // ½ûÓÃÈ«¾ÖAjaxʼþ. });
Èç¹ûÄãÏëΪȫ¾ÖAJAXÉèÖòÎÊý£¬Äã»áÓÃÉÏajaxSetup()º¯Êý¡£ÀýÈ磬½«ËùÓÐAJAXÇëÇ󶼴«µÝµ½request.php£¬£»½ûÓÃÈ«¾Ö·½·¨£»Ç¿ÖÆÓÃPOST·½·¨´«µÝ£º
$.ajaxSetup({ url: "request.php", global: false, type: "POST" });
һЩÄã²»µÃ²»ÖªµÀµÄ·½·¨
дAJAX¿Ï¶¨Àë²»¿ª´ÓÒ³Ãæ»ñÈ¡ÏàÓ¦µÄÖµ¡£ÔÚÕâÀï¼òµ¥ÁоÙһЩ·½·¨£º
- val()
- val()º¯Êý¿ÉÒÔ·µ»Ø±íµ¥×齨µÄÖµ£¬ÀýÈçÈκÎÖÖÀàinputµÄÖµ¡£ÅäºÏÑ¡Ôñ·û²Ù×÷£¬Äã¿ÉÒÔÇáÒ×»ñÈ¡Ñ¡Ïî×é¡¢ÊäÈë¿ò¡¢°´Å¥µÈÔªËصÄÖµ¡£
$("input[name='info']:text").val(); //·µ»ØÃû×ÖΪinfoµÄÎı¾¿òµÄÖµ $("input[name='pass']:password").val(); //·µ»ØÃû×ÖΪpassµÄÃÜÂë¿òµÄÖµ $("input[name='save']:radio").val(); //·µ»ØÃû×ÖΪsaveµÄµ¥Ñ¡ÏîµÄÖµ //ÒÔ´ËÀàÍÆ
- serialize()
- serializeº¯Êý¿ÉÒÔ°ïÄã°Ñ±íµ¥¶ÔÏóµÄËùÓÐÖµ¶¼×ª»»Îª×Ö·û´®ÐòÁС£Èç¹ûÄãҪдGET¸ñʽµÄÇëÇó£¬Õâ¸ö¾Í·Ç³£·½±ãÁË¡£
- serializeArray()
- ºÍserialize()ÀàËÆ£¬Ö»²»¹ýËü·µ»ØµÄÊÇJSON¶ÔÏó¡£
AJAXÓ¦Óòο¼
(WordPress) ʹÓà jQuery ʵÏÖ Ajax ÁôÑÔ : http://hellobmw.com/archives/ajax-comments-with-jquery-for-wordpress.html
ÀûÓÃJQuery·½±ãʵÏÖ»ùÓÚAjaxµÄÊý¾Ý²éѯ¡¢ÅÅÐòºÍ·ÖÒ³¹¦ÄÜ : http://www.malsup.com/jquery/form/
http://www.cnblogs.com/henryfan/archive/2007/01/27/631954.html
jQuery – ajaxÎÞˢб£´æÀý×Ó : http://www.phpchina.com/1296/viewspace_12580.html
²Î¿¼ÎÄÏ×
ʹÓà jQuery ¼ò»¯ Ajax ¿ª·¢ : http://www.ibm.com/developerworks/cn/xml/x-ajaxjquery.html
jQuery Official Documention : http://docs.jquery.com/Ajax
jQuery Ajax È«½âÎö : http://www.cnblogs.com/QLeelulu/archive/2008/04/21/1163021.html
jQueryÖÐAjaxʼþ : http://shawphy.com/2008/08/ajax-event-in-jquery.html