Ajax½éÉÜ XMLHttpRequest
Ajax½éÉÜ
AJAX - ä¯ÀÀÆ÷Ö§³Ö
AJAX µÄÒªµãÊÇ XMLHttpRequest ¶ÔÏó¡£²»Í¬µÄä¯ÀÀÆ÷´´½¨ XMLHttpRequest ¶ÔÏóµÄ·½·¨ÊÇÓвîÒìµÄ¡£IE ä¯ÀÀÆ÷ʹÓà ActiveXObject£¬¶øÆäËûµÄä¯ÀÀÆ÷ʹÓÃÃûΪ XMLHttpRequest µÄ JavaScript ÄÚ½¨¶ÔÏó¡£ÈçÐèÕë¶Ô²»Í¬µÄä¯ÀÀÆ÷À´´´½¨´Ë¶ÔÏó£¬ÎÒÃÇҪʹÓÃÒ»Ìõ "try and catch" Óï¾ä¡£
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}
catch (e) {
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");// Internet Explorer 6.0+
}
catch (e) {
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");// Internet Explorer 5.5
}
catch (e) {
alert("ÄúµÄä¯ÀÀÆ÷²»Ö§³ÖAJAX£¡");
return false;}
}}
}
</script>
ÉùÃ÷Ò»¸ö±£´æ XMLHttpRequest ¶ÔÏóµÄ xmlHttp ±äÁ¿¡£È»ºóʹÓà XMLHttp=new XMLHttpRequest() À´´´½¨´Ë¶ÔÏó¡£ÕâÌõÓï¾äÕë¶Ô Firefox¡¢Opera ÒÔ¼° Safari ä¯ÀÀÆ÷¡£¼ÙÈçʧ°Ü£¬Ôò³¢ÊÔInternet Explorer 6.0+ µÄ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")£¬¼ÙÈçÒ²²»³É¹¦£¬Ôò³¢ÊÔInternet Explorer 5.5+ µÄ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")¡£¼ÙÈçÕâÈýÖÖ·½·¨¶¼²»Æð×÷Óã¬ÄÇôÕâ¸öÓû§ËùʹÓõÄä¯ÀÀÆ÷ÒѾ̫¹ýʱÁË£¬Ëû»òËý»á¿´µ½Ò»¸öÉùÃ÷´Ëä¯ÀÀÆ÷²»Ö§³Ö AJAX µÄÌáʾ¡£
AJAX - XMLHttpRequest¶ÔÏó
onreadystatechange ÊôÐÔ
onreadystatechange ÊôÐÔ´æÓд¦Àí·þÎñÆ÷ÏìÓ¦µÄº¯Êý¡£ÏÂÃæµÄ´úÂ붨ÒåÒ»¸ö¿ÕµÄº¯Êý£¬¿Éͬʱ¶Ô onreadystatechange ÊôÐÔ½øÐÐÉèÖãº
xmlHttp.onreadystatechange=function()
{
// ÎÒÃÇÐèÒªÔÚÕâÀïдһЩ´úÂë
}
readyState ÊôÐÔ
readyState ÊôÐÔ´æÓзþÎñÆ÷ÏìÓ¦µÄ״̬ÐÅÏ¢¡£Ã¿µ± readyState ¸Ä±äʱ£¬onreadystatechange º¯Êý¾Í»á±»Ö´ÐС££¨ÏêÇé²Î¼û AJAX – readyStateÏêÇ飩
״̬
ÃèÊö
Ïà¹ØÎĵµ£º
²ÎÊýÃû
ÀàÐÍ
ÃèÊö
url
String
(ĬÈÏ: µ±Ç°Ò³µØÖ·) ·¢ËÍÇëÇóµÄµØÖ·¡£
type
String
(ĬÈÏ: "GET") ÇëÇó·½Ê½ ("POST" »ò "GET")£¬ ĬÈÏΪ "GET"¡£×¢Ò⣺ÆäËü HTTP ÇëÇó·½·¨£¬Èç PUT ºÍ Delete Ò²¿ÉÒÔʹÓ㬵«½ö²¿·Öä¯ÀÀÆ÷Ö§³Ö¡£
timeout
N ......
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
<meta http-equiv="pragma" content="no-cache"&g ......
AjaxµÄIE»º´æÎÊÌ⣬»áÔì³ÉÓÃopenʱÓÃget·½·¨²»ÄÜÖظ´Ìá½»,µ¼Ö»ñÈ¡²»µ½ÊµÊ±µÄÐÅÏ¢¡£½â¾öµÄ°ì·¨ÓÐÒÔϼ¸ÖÖ£º
1.¿Í»§¶ËÔÚurlºóÌí¼ÓËæ»úÊý.
new Date().getTime()»òÕßMath.Random()
»òÕßËÍÇëÇóÇ°¼ÓÉÏ
XMLHttpRequest.setRequestHeader("If-Modified-Since","0")
2.»òÕßÓÃpost·½·¨Ìá½»  ......
¹ØÓÚajaxµÄÃû×Ö
ajax µÄÈ«³ÆÊÇAsynchronous JavaScript and XML£¬ÆäÖУ¬Asynchronous ÊÇÒì²½µÄÒâ˼£¬ËüÓбðÓÚ´«Í³web¿ª·¢ÖвÉÓõÄͬ²½µÄ·½Ê½¡£
¹ØÓÚͬ²½ºÍÒì²½
Òì²½´«ÊäÊÇÃæÏò×Ö·ûµÄ´«Ê䣬ËüµÄµ¥Î»ÊÇ×Ö·û£»¶øͬ²½´«ÊäÊÇÃæÏò±ÈÌصĴ«Ê䣬ËüµÄµ¥Î»ÊÇè壬Ëü´«ÊäµÄʱºòÒªÇó½ÓÊÜ·½ºÍ·¢ËÍ·½µÄʱÖÓÊ ......
½ñÌì³õ´Î½Ó´¥AjaxÄÇôÎÒ½«ÎÒµÄѧϰ¼Ç¼ÏÂÀ´£¬×î½ü¼ÇÐÔ²»Ì«ºÃ¡£±¸Íü°É
1¡¢Ê×ÏÈ´ÓÍø·ÉÏÏÂÔØAjaxÀ©Õ¹°ü£¬VS2008Ö»ÊÇ´ø×Å×î»ù´¡µÄÈý¸öAjax¿Ø¼þ¡£ÄÇôÎÒÃÇÏÂÔØAjaxControlToolkit¿Ø¼þ£¬½«ÏÂÔغóµÄѹËõ°ü½âѹ¡£ÕÒ³öAjaxControlToolkit.dllËùÔÚλÖá£
2¡¢ÔÚVS2008ÖУ¬ËæÒâÕÒÒ»¸öÑ¡Ï£¬»òÕߣ¬Ð½¨Ò»¸öÑ¡ÏÀ´·ÅÈëÎÒÃÇËù¼ÓÈëµÄ ......