手机版

基于jquery异步传输json数据格式实例代码

时间:2021-11-13 来源:互联网 编辑:宝哥软件园 浏览:

1.jsp代码如下复制代码代码如下:%@页面语言=' Java '内容类型=' text/html;charset=UTF-8 "页面编码="UTF-8"% %字符串路径=请求。getcontextpath();字符串基本路径=请求。getscheme()' ://'请求。getservername()“:”请求。getserverport()“路径”/;%!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN ' ' http://www .w3。org/TR/HTML 4/松散。DTD ' HTML hearteta http-equiv=' Content-Type ' Content=' text/HTML;charset=UTF-8 ' title在此插入title/title脚本类型=' text/JavaScript ' src=' http :%=基本路径% js/jquery-1。8 .1 .js '/script/head script type=' text/JavaScript ' $(function(){ $(' # getResult ')).单击(函数(){$).ajax({type:'post ',url: ' %=basePath % jsonAction!getData.action ',dataType:'json ',data : { ' param 1 ' : $(' # param 1 ').attr('value '),' param2':$('#param2 ').attr('value')},success :函数(返回数据){ var html=' table border=' 1 ' trtd编号/tdtd姓名/tdtd描述/TD/tr ';for(var I=0;I returnData . lengthi){ html=' trtd ' returnData[I].id '/tdtd' returnData[i].名称/' tdtd '返回数据[i].描述/TD/tr ';} $(“# result”).html(html);}});});});/script正文输入类型=' text '值=' haha ' id=' param 1 '输入类型=' text '值=' heha ' id=' param 2 ' div id=' result '/divinput类型=' button '值='获取id='getResult'/body/html2 .访问行为代码如下复制代码代码如下:公共类JsonAction扩展操作支持{ public void getData()}引发IOException { httpersvletrequest req=servletactioncontext。getrequest();字符串P1=请求。GetParameter(' param 1 ');字符串p2=请求。GetParameter(' param 2 ');HttpServletResponse rep=servletactioncontext。getresponse();rep . SetContentType(' text/JSON;charset=utf-8 ');PrintWriter pw=rep . Gettwriter();字符串数据='[{ \ ' id ' : ' 01 \ ',\ ' name \ ' : \ ' zhongqian \ ',\ ' description ' : \ ' ' p2 ' \ ' },{ \ ' id ' : ' 01 \ ',\ ' name \ ' : \ ' zhangsan \ ',\ ' description ' : \ ' ' p2 ' \ ' } ']';pw.print(数据);pw。flush();}}3.效果如下

版权声明:基于jquery异步传输json数据格式实例代码是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。