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; }