ajax½øÐÐÓû§µÄÓÐЧÐÔ¼ì²âʾÀý
ajax½øÐÐÓû§µÄÓÐЧÐÔ¼ì²âʾÀý
1¡¢ ÊäÈëÓò£º
»áÔ±µÄÓÐЧÐÔ¼ì²é£º
<input type="text" name="username"> <input type="button" value="¼ì²é" onclick="return check();">
2¡¢ JavaScript½Å±¾£º
var xmlHttp;
//´´½¨XMLHttpRequest¶ÔÏó
function createXMLHttpRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpReuqest();
}
//×¢²áÃûÓÐЧÐÔ¼ì²é
function check() {
var username = document.getElementById('username').value;
if(username=="") {
alert("Óû§Ãû²»ÄÜΪ¿Õ£¡");
return;
}
createXMLHttpRequest();
xmlHttp.onreadystatechange = processCheck;
xmlHttp.open("GET", "CheckUser?username=" + username);
xmlHttp.send(null);
}
//°ó¶¨µÄÓÐЧÐÔ¼ì²éº¯Êý
function processCheck() {
var result;
if(xmlHttp.readyState == 4) { //Èç¹ûÏìÓ¦Íê³É
if(xmlHttp.status == 200) { //Èç¹û·µ»Ø³É¹¦
result = xmlHttp.responseText;
&
Ïà¹ØÎĵµ£º
AjaxÌá½»Êý¾ÝÒ»°ã¶¼ÊÇͨ¹ýURLºó±ßµÄ²ÎÊýÐÎʽÀ´´«µÝÊý¾ÝµÄ£¬±ÈÈçʹÓÃXMLHttpRequest.open('POST', url, true);µÄ·½Ê½Ê¹ÓÃʱ£¬Æäurl²ÎÊýÖоͰüº¬ÁËÐèÒªÌá½»µÄÊý¾Ý£¬ËüµÄÐÎʽÈ磺http://www.mysite.com/handler.do?name='²Ü²Ù'&sex='ÄÐ'£¬ÊǰÑkey=valueÐÎʽµÄÊý¾Ýͨ¹ý &·ûºÅÁ¬½ÓÆðÀ´£¬ ......
var jsonObj;
var xmlhttp = null;
function callAsync(url, callbackSuccess)
{
xmlhttp = new Ajax();
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange=function()
{
// readyState = 4 ; "complete"
if (xmlhttp.readyState==4)
{ ......
±¾ÎÄÄÚÈÝÕª×Ô£º¡¶Java Web¿ª·¢½Ì³Ì——ÈëÃÅÓëÌá¸ßƪ£¨JSP+Servlet£©¡·
ÏÂÃæÒÔ×¢²á¹ý³ÌÖÐÓû§ÃûÊÇ·ñ´æÔÚµÄÑé֤ΪÀý½éÉÜAJAXµÄÓ¦Óá£
¹¦ÄÜÃèÊö£ºÓû§×¢²áµÄÓû§Ãû²»ÔÊÐíÖØ¸´£¬ËùÒÔÔÚÓû§ÌύʱºòÐèÒªÅжϡ£ÎªÁËÈÃÓû§ÔçÖªµÀ½á¹û£¬ÔÚÓû§ÊäÈëÓû§ÃûÖ®ºó¾ÍÓ¦¸Ã½øÐÐÅжϣ¬¿ÉÒÔ²ÉÓÃAJAX½øÐд¦Àí¡£
¼ÙÉ裺ΪÁ˼ò»¯´ú ......
dataÀàÐÍ
js´úÂ룺
$.post("getmaterial.do?method=getmaterial",
{name:name},
function (data) {
});
ÆäÖÐgetmaterial.do?method=getmaterialÊÇÇëÇóµÄurl£¬{name:name}ÊÇ´«µÝµ½ºǫ́µÄ²ÎÊý£¬¿ÉÒÔÓÃ
request.getParameter("name");»ñÈ¡£¬dataÊǺǫ́´«µÝ¹ýÀ´µÄÊý¾Ý¡£
action´úÂ룺
response.setContentType( ......
ÔÚʹÓÃJQueryµÄajax·ÃÎÊÒ³ÃæÊ±£¬×ÜÊdzö´í£¬µÃ²»µ½·µ»ØµÄhtml´úÂ룬ÓÖ²»ÖªµÀ³öµÄʲô´í£¬°Ù˼²»µÃÆä½â
ÓÚÊǾÍÓÃXMLHTTPÈ¥·ÃÎÊ£¬½á¹ûÒ²³ö´í£¬²»¹ýÄܹ»µÃµ½´íÎóÔÒò£¬È»ºó¸ù¾Ý´íÎóÒòÎªÈ¥ÍøÉϲ飬ÖÕÓڵõ½Á˴𰸡£¡£
ÔÀ´jspµÄ±àÂëÓëjavaµÄ±àÂë²»Ò»ÖÂ
·½°¸¢Ù
´íÎó
<%@ page contentType="text/html;charset= ......