AJAX基础应用一例
JS:
<script>
var xhr = AjaxXmlHttpRequest();
function getSups(){
if(xhr==null){
alert('浏览器不支持Ajax');
return;
}
xhr.onreadystatechange=stateChanged;
var url = "supliers.do?method=getAllSup";
xhr.open("post",url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
function stateChanged(){
//alert("测试");
if(xhr.readyState==4){
document.getElementById("sup").innerHTML=xhr.responseText;
return;
}
}
function AjaxXmlHttpRequest() {
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 false;
}
}
}
return xmlHttp;
}
</script>
JAVA代码
public ActionForward getSups(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Supplier> rlist = this.supServices.getAllSupp();
String beg1 = "<option value= ";
String beg2=">";
String end = "</option>";
for(Supplier sup:rlist){
beg1+=sup.getS_id();
beg1+=beg2;
beg1+=sup.getSupplier_name();
beg1+=end;
}
PrintWriter pw = response.getWriter();
pw.write(beg1);
return null;
}
持续学习、持续收获才能带来持续的满足和快乐!
浙公网安备 33010602011771号