AJax¼¼Êõ

ÉîÈëÀí½âajaxϵÁеھÅƪ£ºjQueryÖеÄajax(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-04-05 11:00 ÎÒÒªÆÀÂÛ( )

Ï൱ÓÚÒ»¸ö Ajax º¯ÊýµÄ¼òдÐÎʽ $.ajax({ type: POST , url: url, data: data, success: success, dataType: dataType});======= $( #send ).click( function () {$.post( jqRequest.php ,{ num : $( #num ).val()

Ï൱ÓÚÒ»¸ö 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 'ÆæÊý'; } ?> È«¾Öʼþ

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Discuz Uchome ajaxpostС¼¼ÇÉ

    Discuz Uchome ajaxpostС¼¼ÇÉ

    2017-04-05 11:01

  • Ò»¸öÇ¿½¡ ʵÓõÄasp+ajax¶þ¼¶Áª¶¯²Ëµ¥(ÓÐÑÝʾºÍ¸½Ô´³ÌÐò´ò°üÏÂÔØ

    Ò»¸öÇ¿½¡ ʵÓõÄasp+ajax¶þ¼¶Áª¶¯²Ëµ¥(ÓÐÑÝʾºÍ¸½Ô´³ÌÐò´ò°üÏÂÔØ

    2017-04-05 10:01

  • Ajax»º´æºÍ±àÂëÎÊÌâµÄ×îÖÕ½â¾ö·½°¸

    Ajax»º´æºÍ±àÂëÎÊÌâµÄ×îÖÕ½â¾ö·½°¸

    2017-04-04 15:03

  • ÓÃajax¸úPHPʵÏÖ¼òµ¥µÄÁ÷³Ì¹ÜÀí

    ÓÃajax¸úPHPʵÏÖ¼òµ¥µÄÁ÷³Ì¹ÜÀí

    2017-04-04 15:00

ÍøÓѵãÆÀ
¼