JavaScriptÖ®AjaxÒì²½

µ¼Óï ͬ²½µ÷ÓùÌÈ»¼òµ¥£¬µ«Ê¹ÓÃÒì²½µ÷ÓòÅÊÇÎÒÃÇÕæÕý³£ÓõÄÊֶΡ£Ê¹ÓÃÒì²½µ÷ÓõÄʱºò£¬ÐèÒª´¥·¢readystatechangeʼþ£¬È»ºó¼ì²âreadyStateÊôÐÔ¼´¿É¡£Õâ¸öÊôÐÔÓÐÎå¸öÖµ£ºÖµ×´Ì¬ËµÃ÷0δ³õʼ»¯ÉÐδµ÷ÓÃopen()·½·¨1Æô¶¯ÒÑ

ͬ²½µ÷ÓùÌÈ»¼òµ¥£¬µ«Ê¹ÓÃÒì²½µ÷ÓòÅÊÇÎÒÃÇÕæÕý³£ÓõÄÊֶΡ£Ê¹ÓÃÒì²½µ÷ÓõÄʱºò£¬ÐèÒª´¥·¢readystatechangeʼþ£¬È»ºó¼ì²âreadyStateÊôÐÔ¼´¿É¡£Õâ¸öÊôÐÔÓÐÎå¸öÖµ£º
Öµ ״̬ ˵Ã÷
0 δ³õʼ»¯ ÉÐδµ÷ÓÃopen()·½·¨
1 Æô¶¯ ÒѾ­µ÷ÓÃopen()·½·¨£¬µ«ÉÐδµ÷ÓÃsend()·½·¨
2 ·¢ËÍ ÒѾ­µ÷ÓÃsend()·½·¨£¬µ«ÉÐδ½ÓÊÜÏìÓ¦
3 ½ÓÊÜ ÒѾ­½ÓÊܵ½²¿·ÖÏìÓ¦Êý¾Ý
4 Íê³É ÒѾ­½ÓÊܵ½È«²¿ÏìÓ¦Êý¾Ý£¬¶øÇÒ¿ÉÒÔʹÓÃ
 
addEvent(document, 'click', function () {
var xhr = new createXHR();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert(xhr.responseText);
} else {
alert('Êý¾Ý·µ»Øʧ°Ü£¡×´Ì¬´úÂ룺' + xhr.status + '״̬ÐÅÏ¢£º' 
+ xhr.statusText);
}
}
};
xhr.open('get', 'demo.php?rand=' + Math.random(), true);
xhr.send(null);
});
 
PS£ºÊ¹ÓÃabort()·½·¨¿ÉÒÔÈ¡ÏûÒì²½ÇëÇ󣬷ÅÔÚsend()·½·¨Ö®Ç°»á±¨´í¡£·ÅÔÚresponseText֮ǰ»áµÃµ½Ò»¸ö¿ÕÖµ¡£
 
Ò»£®GETÓëPOST
ÔÚÌṩ·þÎñÆ÷ÇëÇóµÄ¹ý³ÌÖУ¬ÓÐÁ½ÖÖ·½Ê½£¬·Ö±ðÊÇ£ºGETºÍPOST¡£ÔÚAjaxʹÓõĹý³ÌÖУ¬GETµÄʹÓÃƵÂÊÒª±ÈPOST¸ß¡£
ÔÚÁ˽âÕâÁ½ÖÖÇëÇó·½Ê½Ç°£¬ÎÒÃÇÏÈÁ˽âÒ»ÏÂHTTPÍ·²¿ÐÅÏ¢£¬°üº¬·þÎñÆ÷·µ»ØµÄÏìӦͷÐÅÏ¢ºÍ¿Í»§¶Ë·¢ËͳöÈ¥µÄÇëÇóÍ·ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔ»ñÈ¡ÏìӦͷÐÅÏ¢»òÕßÉèÖÃÇëÇóÍ·ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔÔÚFirefoxä¯ÀÀÆ÷µÄfirebug²é¿´ÕâЩÐÅÏ¢¡£
//ʹÓÃgetResponseHeader()»ñÈ¡µ¥¸öÏìӦͷÐÅÏ¢
alert(xhr.getResponseHeader('Content-Type'));
 
//ʹÓÃgetAllResponseHeaders()»ñÈ¡Õû¸öÏìӦͷÐÅÏ¢
alert(xhr.getAllResponseHeaders());
 
//ʹÓÃsetRequestHeader()ÉèÖõ¥¸öÇëÇóÍ·ÐÅÏ¢
xhr.setRequestHeader('MyHeader', 'Lee'); //·ÅÔÚopen·½·¨Ö®ºó£¬send·½·¨Ö®Ç°
 
PS£ºÎÒÃÇÖ»¿ÉÒÔ»ñÈ¡·þÎñÆ÷·µ»Ø»ØÀ´ÏìӦͷÐÅÏ¢£¬ÎÞ·¨»ñÈ¡Ïò·þÎñÆ÷Ìá½»µÄÇëÇóÍ·ÐÅÏ¢£¬×ÔÈ»×Ô¶¨ÒåµÄÇëÇóÍ·£¬ÔÚJavaScript¶ËÊÇÎÞ·¨»ñÈ¡µ½µÄ¡£
 
GETÇëÇó
GETÇëÇóÊÇ×î³£¼ûµÄÇëÇóÀàÐÍ£¬×î³£ÓÃÓÚÏò·þÎñÆ÷²éѯijЩÐÅÏ¢¡£±ØҪʱ£¬¿ÉÒÔ½«²éѯ×Ö·û´®²ÎÊý×·¼Óµ½URLµÄĩ⣬ÒÔ±ãÌá½»¸ø·þÎñÆ÷¡£
xhr.open('get', 'demo.php?rand=' + Math.random() + '&name=Koo', true);
 
ͨ¹ýURLºóµÄÎʺŸø·þÎñÆ÷´«µÝ¼üÖµ¶ÔÊý¾Ý£¬·þÎñÆ÷½ÓÊÕµ½·µ»ØÏìÓ¦Êý¾Ý¡£ÌØÊâ×Ö·û´«²Î²úÉúµÄÎÊÌâ¿ÉÒÔʹÓÃencodeURIComponent()½øÐбàÂë´¦Àí£¬ÖÐÎÄ×Ö·ûµÄ·µ»Ø¼°´«²Î£¬¿ÉÒÔ½²Ò³Ãæ±£´æºÍÉèÖÃΪutf-8¸ñʽ¼´¿É¡£
//Ò»¸öͨÓõÄURLÌá½»º¯Êý
function addURLParam(url, name, value) {
url += (url.indexOf('?') == -1 ? '?' : '&'); //ÅжϵÄurlÊÇ·ñÓÐÒÑÓвÎÊý
url += encodeURIComponent(name) + '=' + encodeURIComponent(value);
alert(url);
return url;
}
 
PS£ºµ±Ã»ÓÐencodeURIComponent()·½·¨Ê±£¬ÔÚһЩÌØÊâ×Ö·û±ÈÈç“&”£¬»á³öÏÖ´íÎóµ¼ÖÂÎÞ·¨»ñÈ¡¡£
 
POSTÇëÇó
POSTÇëÇó¿ÉÒÔ°üº¬·Ç³£¶àµÄÊý¾Ý£¬ÎÒÃÇÔÚʹÓÃ±íµ¥Ìá½»µÄʱºò£¬ºÜ¶à¾ÍÊÇʹÓõÄPOST´«Ê䷽ʽ¡£
xhr.open('post', 'demo.php', true);
 
¶ø·¢ËÍPOSTÇëÇóµÄÊý¾Ý£¬²»»á¸úÔÚURLµÄβ°ÍÉÏ£¬¶øÊÇͨ¹ýsend()·½·¨Ïò·þÎñÆ÷Ìá½»Êý¾Ý¡£
xhr.send('name=Lee&age=100');
 
Ò»°ãÀ´Ëµ£¬Ïò·þÎñÆ÷·¢ËÍPOSTÇëÇóÓÉÓÚ½âÎö»úÖƵÄÔ­Òò£¬ÐèÒª½øÐÐÌرðµÄ´¦Àí¡£ÒòΪPOSTÇëÇóºÍWeb±íµ¥Ìá½»ÊDz»Í¬µÄ£¬ÐèҪʹÓÃXHRÀ´Ä£·Â±íµ¥Ìá½»¡£
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 
PS£º´ÓÐÔÄÜÉÏÀ´½²POSTÇëÇó±ÈGETÇëÇóÏûºÄ¸ü¶àһЩ£¬ÓÃÏàͬÊý¾Ý±È½Ï£¬GET×î¶à±ÈPOST¿ìÁ½±¶¡£
 
 JSONÒ²¿ÉÒÔʹÓÃAjaxÀ´»Øµ÷·ÃÎÊ¡£
var url = 'demo.json?rand=' + Math.random();
var box = JSON.parse(xhr.responseText);
http://www.aseoe.com/ true JavaScriptÖ®AjaxÒì²½ http://www.aseoe.com/show-12-319-1.html report <£¿php echo strlen($content) / 2; ?> ͬ²½µ÷ÓùÌÈ»¼òµ¥£¬µ«Ê¹ÓÃÒì²½µ÷ÓòÅÊÇÎÒÃÇÕæÕý³£ÓõÄÊֶΡ£Ê¹ÓÃÒì²½µ÷ÓõÄʱºò£¬ÐèÒª´¥·¢readystatechangeʼþ£¬È»ºó¼ì²âreadyStateÊôÐÔ¼´¿É¡£Õâ¸öÊôÐÔÓÐÎå¸öÖµ£ºÖµ×´Ì¬ËµÃ÷0δ³õʼ»¯ÉÐδµ÷ÓÃopen()·½·¨1Æô¶¯ÒÑ
TAG:JavaScript Ajax Òì²½
±¾Õ¾»¶Ó­ÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-319-1.html

[Ç°¶Ë²å¼þÍƼö] Plugin

1 2 3 4
  • jQueryʵÏÖÖð×ÖÖð¾äÏÔʾ²å¼þl-by-l.min.js
  • jQuery´ø·½Ïò¸ÐÖªµÄÊó±ê»¬¹ýͼƬ±ß¿òÌØЧ²å¼þ
  • jQuery HotKeys¼àÌý¼üÅÌ°´ÏÂʼþkeydown²å¼þ
  • ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
webÇ°¶Ë¿ª·¢
°®Ë¼×ÊÔ´Íø Copyright 2012-2014 Www.Aseoe.Com All rights reserved.(½úICP±¸13001436ºÅ-1)