JSON

Ajax与JSON技术

字号+ 作者:H5之家 来源:H5之家 2017-07-07 15:08 我要评论( )

Ajax与JSON技术,Ajax实现简单的验证:htmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/t

正文

Ajax实现简单的验证:

<html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"> <title>Inserttitlehere</title> <scripttype="text/javascript"> functionajaxValid(){ varxhr=null; varusername=document.getElementById("username").value; //创建XMLHttpRequst对象,今后主要靠此对象进行与后台交互 if(window.ActiveXObject){ //IE5,6创建对象的方式 xhr=newActiveXObject("Microsoft.XMLHTTP"); }else{ xhr=newXMLHttpRequest(); } //打开连接 xhr.open("get",'/Web030Ajax/AjaxServlet?username='+username); //发送请求 xhr.send(null); xhr.onreadystatechange=function(){ //readyState码,0代表未初始化,1正在加载2已加载3正在交互4完成 if(xhr.readyState==4){ //服务器响应码,200成功 if(xhr.status==200){ // console.log('成功'); varjsondata=JSON.parse(xhr.responseText); //alert(jsondata.info); document.getElementById("info").innerHTML=jsondata.info; } } }; } </script> </head> <body> <formaction=""> <table> <tr> <td>用户名</td> <td><inputtype="text"name="username"id="username"onblur="ajaxValid()"/><spanid="info"></span></td> </tr> <tr> <td>密码</td> <td><inputtype="password"name="password"id="password"/></td> </tr> </table> </form> </body> </html>

servlet端

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub Stringusername=request.getParameter("username"); PrintWriterout=response.getWriter(); if(username.equals("admin")){ out.print("{\"info\":\"exit\"}"); }else{ out.print("{'info':'ok'}"); } }

省份

<head> <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"> <title>Inserttitlehere</title> <scripttype="text/javascript"> functionff(){ varselected=document.getElementById("selected1"); selected.onclick=function(){ varxhr=null; xhr=newXMLHttpRequest(); xhr.open("post","/Web030Ajax/Province"); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//注意这句话的顺序 xhr.send(null); xhr.onreadystatechange=function(){ if(xhr.readyState==4){ varprostr=xhr.responseText; vararry=JSON.parse(prostr); document.getElementById("selected").innerHTML=''; for(vari=0;i<arry.length;i++){ document.getElementById("selected").innerHTML+='<option>'+arry[i]+'</option>'; } } }; } } </script> </head> <bodyonload="ff()"> <formaction=""> <selectid="selected"> </select> <inputtype="button"value="dianwo"id="selected1"> </form> </body> </html>

servlet端

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //TODOAuto-generatedmethodstub response.setCharacterEncoding("UTF-8"); PrintWriterout=response.getWriter(); List<String>provinces=newArrayList<String>(); provinces.add("山东"); provinces.add("北京"); provinces.add("上海"); Stringjsondata=JSONArray.fromObject(provinces).toString(); out.print(jsondata); out.close(); }


 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Node.js实现Excel转JSON

    Node.js实现Excel转JSON

    2017-07-07 14:00

  • 基于Json.NET自己实现MVC中的JsonValueProviderFactory

    基于Json.NET自己实现MVC中的JsonValueProviderFactory

    2017-07-07 13:02

  • 在Unity中读写文件数据:LitJSON快速教程

    在Unity中读写文件数据:LitJSON快速教程

    2017-07-06 17:03

  • React系列学习笔记:5.PC与MAC通用package.json配置

    React系列学习笔记:5.PC与MAC通用package.json配置

    2017-07-06 17:01

网友点评
e