1.jquery ajaxçreadyStateåstatusçåºå«å使ç¨
jquery ajaxçreadyStateåstatusçåºå«å使ç¨
å¨åå ç¯åæäºjqueryçajaxå¼æ¥ååæ¥ï¼ä»¥åå¼å¸¸çä¸äºå¤çï¼æè§è¿æ²¡ææajaxçreadyStateåstatusè¯´æ¸ æ¥.ä»å¤©å°±æ¥è¯´è¯´ajaxç¶æçé£ç¹äºã
jquery ajaxå½æ°æºä»£ç æ¯è¿æ ·çï¼
var getXmlHttpRequest = function () {
if (window.XMLHttpRequest) {
//主æµæµè§å¨æä¾äºXMLHttpRequest对象
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
//ä½çæ¬çIEæµè§å¨æ²¡ææä¾XMLHttpRequest对象
//æä»¥å¿ é¡»ä½¿ç¨IEæµè§å¨çç¹å®å®ç°ActiveXObject
return new ActiveXObject("Microsoft.XMLHTTP");
}
};
var xhr = getXmlHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === ) {
//è·åæååæ§è¡æä½
//æ°æ®å¨xhr.responseText
}
};
xhr.open("TYPE", "URL", true);
xhr.send("");
ä»ä¹æ¯readyState
readyStateæ¯XMLHttpRequest对象çä¸ä¸ªå±æ§ï¼ç¨æ¥æ è¯å½åXMLHttpRequest对象å¤äºä»ä¹ç¶æã
readyStateæ»å ±æ5个ç¶æå¼ï¼åå«ä¸º0~4ï¼æ¯ä¸ªå¼ä»£è¡¨äºä¸åçå«ä¹ï¼å¦ä¸è¡¨æ示ï¼
0 æªåå§åç¶æï¼æ¤æ¶ï¼å·²ç»å建äºä¸ä¸ªXMLHttpRequest对象
1 åå¤åéç¶æï¼æ¤æ¶ï¼å·²ç»è°ç¨äºXMLHttpRequest对象çopenæ¹æ³ï¼å¹¶ä¸XMLHttpRequest对象已ç»åå¤å¥½å°ä¸ä¸ªè¯·æ±åéå°æå¡å¨ç«¯
2 å·²ç»åéç¶æï¼æ¤æ¶ï¼å·²ç»éè¿sendæ¹æ³æä¸ä¸ªè¯·æ±åéå°æå¡å¨ç«¯ï¼ä½æ¯è¿æ²¡ææ¶å°ä¸ä¸ªååº
3 æ£å¨æ¥æ¶ç¶æï¼æ¤æ¶ï¼å·²ç»æ¥æ¶å°HTTPååºå¤´é¨ä¿¡æ¯ï¼ä½æ¯æ¶æ¯ä½é¨åè¿æ²¡æå®å ¨æ¥æ¶å°
4 å®æååºç¶æï¼æ¤æ¶ï¼å·²ç»å®æäºHTTPååºçæ¥æ¶
ä»ä¹æ¯status
statusæ¯XMLHttpRequest对象çä¸ä¸ªå±æ§ï¼è¡¨ç¤ºååºçHTTPç¶æç ã
å¨HTTP1.1åè®®ä¸ï¼HTTPç¶æç æ»å ±å¯å为5大类ï¼å¦ä¸è¡¨æ示ï¼
1XX æå¡å¨æ¶å°è¯·æ±ï¼éè¦ç»§ç»å¤çãä¾å¦ç¶æç ï¼è¡¨ç¤ºæå¡å¨å°éç¥å®¢æ·ç«¯ä½¿ç¨æ´é«çæ¬çHTTPåè®®ã
2XX 请æ±æåãä¾å¦ç¶æç ï¼è¡¨ç¤ºè¯·æ±æå¸æçååºå¤´ææ°æ®ä½å°éæ¤ååºè¿åã
3XX éå®åãä¾å¦ç¶æç ï¼è¡¨ç¤ºä¸´æ¶éå®åï¼è¯·æ±å°å å«ä¸ä¸ªæ°çURLå°åï¼å®¢æ·ç«¯å°å¯¹æ°çå°åè¿è¡GET请æ±ã
4XX 客æ·ç«¯é误ãä¾å¦ç¶æç ï¼è¡¨ç¤ºå®¢æ·ç«¯è¯·æ±çèµæºä¸åå¨ã
5XX æå¡å¨é误ãä¾å¦ç¶æç ï¼è¡¨ç¤ºæå¡å¨éå°äºä¸ä¸ªæªæ¾é¢æçæ åµï¼å¯¼è´äºå®æ æ³å®æååºï¼ä¸è¬æ¥è¯´ï¼è¿ä¸ªé®é¢ä¼å¨ç¨åºä»£ç åºéæ¶åºç°ã
æåºé®é¢
为ä»ä¹onreadystatechangeçå½æ°å®ç°è¦åæ¶å¤æreadyStateåstatuså¢ï¼
æ们ç¥é readyState === 4 å·²ç»è¡¨ç¤ºäºè¯·æ±ååºæåäºï¼ä¸ºä»ä¹è¿è¦åç»çstatuså¢ï¼å¸¦çé®é¢ï¼æ们å¼å§æ¥åä¸äºè¯éªå§ã
åªä½¿ç¨readyStateå¤æ
javascript端çå®ç°ä»£ç å¦ä¸ï¼
var getXmlHttpRequest = function () {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
};
var xhr = getXmlHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
alert(xhr.responseText);
}
};
xhr.open("GET", "/data.aspx", true);
xhr.send("");
æ们å¨æå¡ç«¯æåºå¼å¸¸ï¼
public partial class data : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
throw new Exception("Error");
}
}
è¿è¡javascript代ç ï¼æ示çªå£åºç°äºå¦ä¸ï¼
ITå享
æå¡ååºåºéäºï¼ä½è¿æ¯è¿åäºä¿¡æ¯ï¼è¿å¹¶ä¸æ¯æ们æ³è¦çç»æãæå¼Fiddlerçæ§ï¼å¯ä»¥çå°data.aspxè¿åçæ¯ååºï¼ä½ç±äºåªä½¿ç¨ readystateåå¤æï¼å®ä¸çä¼æ¾åçç»ææ¯è¿æ¯ï¼åªè¦ååºæåè¿åäºï¼å°±æ§è¡æ¥ä¸æ¥çjavascript代ç ï¼ç»æå°é æåç§ä¸å¯ é¢æçé误ãæ以åªä½¿ç¨readyStateå¤ææ¯è¡ä¸éçã
æ¢å¦å¤ä¸ä¸ªè§åº¦æ³ï¼ç¶æç è¿å就表示è¿æ¬¡ååºæ¯æåçäºï¼é£ä¹æ¯ä¸æ¯å¯ä»¥ä¸ä½¿ç¨readyStateï¼åç¬åªä½¿ç¨statusåå¤æå¢ï¼å¥½ï¼å¸¦çé®é¢ï¼ç»§ç»æ¥åè¯éªå§ã
åªä½¿ç¨statuså¤æ
javascript端ç代ç å®ç°å¦ä¸ï¼
var getXmlHttpRequest = function () {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
};
var xhr = getXmlHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.status === ) {
alert("readyState=" + xhr.readyState + xhr.responseText);
}
};
xhr.open("GET", "/data.aspx", true);
xhr.send("");
äº å®ä¸ï¼ç»æå´ä¸åé¢æé£æ ·ãååºç ç¡®å®æ¯è¿åäºï¼ä½æ¯æ»å ±å¼¹åºäº3次çªå£ï¼ç¬¬ä¸æ¬¡æ¯âreadyState=2âççªå£ï¼ç¬¬äºæ¬¡æ¯ âreadyState=3Testâççªå£ï¼ç¬¬ä¸æ¬¡æ¯âreadyState=4Testâççªå£ãç±æ¤ï¼å¯è§onreadystatechangeå½ æ°çæ§è¡ä¸æ¯åªå¨readyStateå为4çæ¶å触åçï¼èæ¯readyStateçæ¯æ¬¡ååé½ä¼è§¦åï¼æ以就åºç°äºåé¢è¯´çé£ç§æ åµãå¯è§ï¼åç¬ä½¿ç¨ statuså¤æä¹æ¯è¡ä¸éçã
è¿ä¸æ¥æè
ç±ä¸é¢çè¯éªï¼æ们å¯ä»¥ç¥éå¤æçæ¶åreadyStateå status缺ä¸ä¸å¯ãé£ä¹readyStateåstatusçå åå¤æ顺åºä¼ä¸ä¼æå½±åå¢ï¼æ们å¯ä»¥å°statusè°å°åé¢å å¤æï¼ä»£ç å¦ xhr.status === && xhr.readyState === 4ã
äºå®ä¸ï¼è¿å¯¹äºæç»çç»æ æ¯æ²¡æå½±åçï¼ä½æ¯ä¸é´çæ§è½å°±ä¸åäºãç±ä¸ä¸ä¸ªè¯éªæ们ç¥éï¼readyStateçæ¯æ¬¡ååé½ä¼è§¦åonreadystatechangeå½æ°ï¼åå¦ å å¤æstatusï¼é£ä¹æ¯æ¬¡é½ä¼å¤å¤æä¸æ¬¡statusçç¶æãè½ç¶æ§è½ä¸å½±åçå¾®ï¼ä¸è¿æ们è¿æ¯åºè¯¥æ±ç追æ±æè´ä»£ç çæ³æ³ï¼æreadyState çå¤ææ¾å¨åé¢ã