Ï൱ÓÚÒ»¸ö Ajax º¯ÊýµÄ¼òдÐÎʽ
$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType }); =======> $('#send').click(function(){ $.post('jqRequest.php',{ num: $('#num').val() },function(data){ $('#result').html('ÄúÑ¡ÔñµÄÊý×Ö' + $('#num').val() + 'ÊÇ' + data) }) }) $num = $_REQUEST['num']; if($num % 2 == 0){ echo 'żÊý'; }else{ echo 'ÆæÊý'; } ?>
post()·½·¨ºÍget()·½·¨µÄ½á¹¹ºÍʹÓ÷½Ê½¶¼Ïàͬ¡£µ«Òª×¢ÒâµÄÊÇ£¬µ±load()·½·¨´øÓÐÊý¾Ý²ÎÊý´«µÝʱ£¬»áʹÓÃPOST·½Ê½·¢ËÍÇëÇó
ÐòÁл¯µ±Ò»¸ö±íµ¥ÖÐ×ֶν϶࣬±íµ¥ÔªËؽϸ´ÔÓʱ£¬¾ÍÐèÒªÒ»ÖÖ·½·¨À´¼ò»¯ÌáÈ¡±íµ¥ÄÚ²¿¿Ø¼þµÄÖµµÄ²Ù×÷£¬ÕâÒ»ÐÐΪͨ³£½ÐÐòÁл¯£¬jQueryÌṩÁËparam()¡¢serialize()ºÍserialzeArray()ÕâÈý¸ö·½·¨
¡¾param()¡¿
param(obj)·½·¨ÓÃÀ´´´½¨Ò»¸öÊý×é»ò¶ÔÏóÐòÁл¯µÄ×Ö·û´®£¬ÊÊÓÃÓÚÒ»¸öURLµØÖ·²éѯ×Ö·û´®»òAjaxÇëÇó
console.log($.param({ width:1680, height:1050 }));//'width=1680&height=1050'¡¾serialize()¡¿
serialize()·½·¨½«ÓÃ×÷Ìá½»µÄ±íµ¥ÔªËصÄÖµ±àÒë³É×Ö·û´®
[×¢Òâ]serialize()·½·¨µÄÒ»¸ö¶îÍâºÃ´¦ÊÇ»á×Ô¶¯¶Ô¼üÖµ¶Ô¶ùÖеÄÌØÊâ×Ö·û½øÐбàÂë
=>Single2/> ===/> ==============>></p> <script> function showValues() { $("#results").text($("form").serialize()); } $(":checkbox, :radio").click(showValues); $("select").change(showValues); showValues(); </script>¡¾serializeArray()¡¿
serializeArray()·½·¨½«ÓÃ×÷Ìá½»µÄ±íµ¥ÔªËصÄÖµ±àÒë³ÉÓµÓÐnameºÍvalue¶ÔÏó×é³ÉµÄÊý×飬¼´json¸ñʽµÄÊý¾Ý¡£ÀýÈç[ { name: a value: 1 }, { name: b value: 2 },...]
==> ==> ==> ==>===> ==> ==> </form> <script> $('form').submit(function() { console.log($(this).serializeArray()); return false; }); </script>½á¹ûÈçÏÂ
[ { name: "a", value: "1" }, { name: "b", value: "2" }, { name: "c", value: "3" }, { name: "d", value: "4" }, { name: "e", value: "5" } ]¸Ã¶ÔÏó¿ÉÒÔʹÓÃeach()º¯Êý¶ÔÊý¾Ý½øÐеü´úÊä³ö
$dataArr = $('form').serializeArray(); $html = ''; $.each($dataArr,function(i,data){ $html += data.name + ':' + data.value + ';'; }) console.log($html);//a:1;b:2;c:3;d:4;e:5; ajax()Ç°Ãæ½éÉܵÄload()¡¢get()¡¢post()¡¢getScript()¡¢getJSON()µÈ·½·¨¶¼ÊÇ»ùÓÚajax()·½·¨ÊµÏÖµÄ
ajax()·½·¨ÊÇ×îµ×²ã¡¢¹¦ÄÜ×îÇ¿´óµÄÇëÇó·þÎñÆ÷Êý¾ÝµÄ·½·¨£¬Ëü²»½ö¿ÉÒÔ»ñÈ¡·þÎñÆ÷·µ»ØµÄÊý¾Ý£¬»¹ÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó²¢´«µÝÊýÖµ£¬ËüµÄµ÷ÓøñʽÈçÏ£º
$.ajax([settings])ÆäÖвÎÊýsettingsΪ·¢ËÍajaxÇëÇóʱµÄÅäÖöÔÏó£¬ÔڸöÔÏóÖУ¬url±íʾ·þÎñÆ÷ÇëÇóµÄ·¾¶£¬dataΪÇëÇóʱ´«µÝµÄÊý¾Ý£¬dataTypeΪ·þÎñÆ÷·µ»ØµÄÊý¾ÝÀàÐÍ£¬successΪÇëÇó³É¹¦µÄÖ´ÐеĻص÷º¯Êý£¬typeΪ·¢ËÍÊý¾ÝÇëÇóµÄ·½Ê½£¬Ä¬ÈÏΪget
³£ÓòÎÊýÈçÏÂËùʾ
Ãû³Æ Öµ/ÃèÊö async ²¼¶ûÖµ£¬±íʾÇëÇóÊÇ·ñÒì²½´¦Àí¡£Ä¬ÈÏÊÇ true¡£ beforeSend(xhr) ·¢ËÍÇëÇóÇ°ÔËÐеĺ¯Êý¡£ cache ²¼¶ûÖµ£¬±íʾä¯ÀÀÆ÷ÊÇ·ñ»º´æ±»ÇëÇóÒ³Ã档ĬÈÏÊÇ true¡£ complete(xhr,status) ÇëÇóÍê³ÉʱÔËÐеĺ¯Êý£¨ÔÚÇëÇó³É¹¦»òʧ°ÜÖ®ºó¾ùµ÷Ó㬼´ÔÚsuccessºÍerrorº¯ÊýÖ®ºó£© contentType ·¢ËÍÊý¾ÝʱʹÓõÄÄÚÈÝÀàÐÍ¡£Ä¬ÈÏÊÇ"application/x-www-form-urlencoded" context ΪËùÓÐ AJAX Ïà¹ØµÄ»Øµ÷º¯Êý¹æ¶¨ "this" Öµ¡£ data ¹æ¶¨Òª·¢Ë͵½·þÎñÆ÷µÄÊý¾Ý¡£ dataFilter(data,type) ÓÃÓÚ´¦Àí XMLHttpRequest ÔʼÏìÓ¦Êý¾ÝµÄº¯Êý¡£ dataType Ô¤ÆڵķþÎñÆ÷ÏìÓ¦µÄÊý¾ÝÀàÐÍ¡£ error(xhr,status,error) Èç¹ûÇëÇóʧ°ÜÒªÔËÐеĺ¯Êý¡£ global ²¼¶ûÖµ£¬¹æ¶¨ÊÇ·ñΪÇëÇó´¥·¢È«¾Ö AJAX ʼþ´¦Àí³ÌÐò¡£Ä¬ÈÏÊÇ true¡£ ifModified ²¼¶ûÖµ£¬¹æ¶¨ÊÇ·ñ½öÔÚ×îºóÒ»´ÎÇëÇóÒÔÀ´ÏìÓ¦·¢Éú¸Ä±äʱ²ÅÇëÇó³É¹¦¡£Ä¬ÈÏÊÇ false¡£ jsonp ÔÚÒ»¸ö jsonp ÖÐÖØд»Øµ÷º¯ÊýµÄ×Ö·û´®¡£ jsonpCallback ÔÚÒ»¸ö jsonp Öй涨»Øµ÷º¯ÊýµÄÃû³Æ¡£ password ¹æ¶¨ÔÚ HTTP ·ÃÎÊÈÏÖ¤ÇëÇóÖÐʹÓõÄÃÜÂë¡£ processData ²¼¶ûÖµ£¬¹æ¶¨Í¨¹ýÇëÇó·¢Ë͵ÄÊý¾ÝÊÇ·ñת»»Îª²éѯ×Ö·û´®¡£Ä¬ÈÏÊÇ true¡£ scriptCharset ¹æ¶¨ÇëÇóµÄ×Ö·û¼¯¡£ success(result,status,xhr) µ±ÇëÇó³É¹¦Ê±ÔËÐеĺ¯Êý¡£ timeout ÉèÖñ¾µØµÄÇëÇó³¬Ê±Ê±¼ä£¨ÒÔºÁÃë¼Æ£©¡£ traditional ²¼¶ûÖµ£¬¹æ¶¨ÊÇ·ñʹÓòÎÊýÐòÁл¯µÄ´«Í³Ñùʽ¡£ type ¹æ¶¨ÇëÇóµÄÀàÐÍ£¨GET »ò POST£©¡£ url ¹æ¶¨·¢ËÍÇëÇóµÄ URL¡£Ä¬ÈÏÊǵ±Ç°Ò³Ãæ¡£ username ¹æ¶¨ÔÚ HTTP ·ÃÎÊÈÏÖ¤ÇëÇóÖÐʹÓõÄÓû§Ãû¡£ xhr ÓÃÓÚ´´½¨ XMLHttpRequest ¶ÔÏóµÄº¯Êý¡£ =======> $('#send').click(function(){ $.ajax({ url:'jqRequest.php', type:'POST', data:{ num:$('#num').val() }, success:function(data){ $('#result').html('ÄúÑ¡ÔñµÄÊý×Ö' + $('#num').val() + 'ÊÇ' + data); } }) }) $num = $_REQUEST['num']; if($num % 2 == 0){ echo 'żÊý'; }else{ echo 'ÆæÊý'; } ?> È«¾Öʼþ
¡¡