ͬ²½µ÷ÓùÌÈ»¼òµ¥£¬µ«Ê¹ÓÃÒì²½µ÷ÓòÅÊÇÎÒÃÇÕæÕý³£ÓõÄÊֶΡ£Ê¹ÓÃÒì²½µ÷ÓõÄʱºò£¬ÐèÒª´¥·¢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/show-12-319-1.html
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-319-1.html