GET:
- var sname = document.queryForm.sname.value;
- var xmlHttp;
- try {
- // Firefox, Opera 8.0+, Safari
- xmlHttp = new XMLHttpRequest();
- } catch (e) {
- // Internet Explorer
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e) {
- alert("您的浏览器不支持AJAX!");
- return;
- }
- }
- }
- xmlHttp.open("GET", "query.do?sname="+sname, true);
- xmlHttp.onreadystatechange=function() {
- if (xmlHttp.readyState==4) {
- result.innerHTML = xmlHttp.responseText;
- //xmlHttp.responseText还有一种写法:xmlHttp.responseXml
- //你可以得到后台传过来的xml,用Javascript来分析,得到需要的数据
- //必须掌握xml的分析技术
- }
- else{
- result.innerHTML = "正在查询,请稍等";
- }
- }
- xmlHttp.send();
POST:
- var url = "/xxx.jsp";
- var postStr = "xxx=" + v;
- var eID = "xxx";
- var xmlHttp;
- try {
- // Firefox, Opera 8.0+, Safari
- xmlHttp = new XMLHttpRequest();
- } catch (e) {
- // Internet Explorer
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e) {
- alert("您的浏览器不支持AJAX!");
- return;
- }
- }
- }
- xmlHttp.open("POST", url, true);
- xmlHttp.setRequestHeader("Content-Type",
- "application/x-www-form-urlencoded");
- xmlHttp.onreadystatechange = function() {
- if (xmlHttp.readyState == 4) {
- if (xmlHttp.responseText == 1) {
- gB(eID).innerHTML = nr + "<font color=blue> 正确</font>"
- } else {
- gB(eID).innerHTML = img + '<b><font color=red>'
- + xmlHttp.responseText + '</font></b>';
- }
- } else {
- gB(eID).innerHTML = "检查中";
- }