可用 js ajax代码

时间:2013-05-13 09:35:58
【文件属性】:
文件名称:可用 js ajax代码
文件大小:2KB
文件格式:TXT
更新时间:2013-05-13 09:35:58
ajax代码 var xmlHttp; function u_reg(field) { var name=field.value; if(""!=name) { xmlHttp=createXmlHttpRequest(); var url="loginAction!reg?username="+name; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=userName; xmlHttp.send(null); } else { return; } } function changeDrop(field) { var typeid=field.value; if(""!=name) { xmlHttp=createXmlHttpRequest(); var url="productAction!reg?username="+typeid; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=initType; xmlHttp.send(null); } else { return; } } function createXmlHttpRequest() { if(window.XMLHttpRequest) { xmlHttp=new XMLHttpRequest(); if(xmlHttp.overideMimeType) { xmlHttp.overidMimeType("text/xml"); } } else if(window.ActiveXobject) { try { xmlHttp=new ActiveObject("Msxml2.XMLHTTP"); } catch(e) { xmlHttp=new ActiveObject("Microsoft.XMLHTTP"); } } if(!xmlHttp) { alert("您的浏览器不支持创建XMLHTTPRequest对象!"); } return xmlHttp; } function userName() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { var u_name=document.getElementById("reg_name"); var uId=xmlHttp.responseText; if(uId=="1") { u_name.innerHTML="用户已存在!"; } else { u_name.innerHTML=""; } } else { alert("请求错误,无法验证用户名是否存在!错误代码:"+xmlHttp.status); } } } function initType() { if(xmlHttp.readyState == 4) {//4:表示Ajax引擎初始化成功 if(xmlHttp.status == 200) {//http协议成功 var typeSelect = document.getElementById("typeid"); var deskLength = typeSelect.options.length; for(var j = deskLength;j > 0;j--) { typeSelect.options.remove(j); } var content8 = xmlHttp.responseText; eval(content8); }else { alert("请求失败,错误码=" + xmlHttp.status); } } } public Object reg() {// 用户注册验证由Ajax实现 OutputStream os = null; PrintWriter pw = null; try { HttpServletResponse res = ServletActionContext.getResponse(); os = res.getOutputStream(); pw = new PrintWriter(os); if (this.userServices.checkUsername(this.getUsername())) { pw.print(1); } else { pw.print(0); } pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { pw.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }

网友评论

  • 我要支持json,这个不支持。