使用post方法实现json往返传输数据的方法
2019-03-31 21:37
585 查看
问题所在:
当我们想让应用层和http之间的所有接口都采用json,这样,客户端代码就可以纯碎用javascript的对象来编写,服务器打啊也可以纯粹的用Java的对象来编写。
我们使用的是post请求的方法,有些不同于get的方法!
客户端html代码:
<html> <head> <title>Hello Ajax version 5a</title> <style type='text/css'> * { font-family: Tahoma, Arial, sans-serif; } #helloTitle{ color: #48f; } .sidebar{ background-color: #adf; color: navy; border: solid blue 1px; width: 180px; height: 200px; padding: 2px; margin: 3px; float: left; } </style> <script type='text/javascript' src='prototype.js'></script> <script type='text/javascript' src='json.js'></script> <script type='text/javascript'> window.onload=function(){ $('helloBtn').onclick=function(){ var name=$('helloTxt').value; new Ajax.Request( "hello5a.jsp", { postBody:JSON.stringify({name:name}), onComplete:function(xhr){ var responseObj=JSON.parse(xhr.responseText); update(responseObj); } } ); } } function update(obj){ $('helloTitle').innerHTML="<h1>Hello, <b><i>"+obj.name+"</i></b></h1>"; var likesHTML='<h5>'+obj.initial+' likes...</h5><hr/>'; for (var i=0;i<obj.likes.length;i++){ likesHTML+=obj.likes[i]+"<br/>"; } $('likesList').innerHTML=likesHTML; var recipeHTML='<h5>'+obj.initial+'\'s favourite recipe</h5>'; for (key in obj.ingredients){ recipeHTML+=key+" : "+obj.ingredients[key]+"<br/>"; } $('ingrList').innerHTML=recipeHTML; } </script> </head> <body> <div id='likesList' class='sidebar'> <h5>Likes</h5><hr/> </div> <div id='ingrList' class='sidebar'> <h5>Ingredients</h5><hr/> </div> <div> <div id='helloTitle'> <h1>Hello, stranger</h1> </div> <p>Please introduce yourself by entering your name in the box below</p> <input type='text' size='24' id='helloTxt'></input> <button id='helloBtn'>Submit</button> </div> </body> </html>
jsp代码:
<jsp:directive.page contentType="application/javascript" import="java.util.*,net.sf.json.*"/> <% //read the request body String json=request.getReader().readLine(); //读取post请求主体 JSONObject jsonObj=new JSONObject(json);//解析json字符串 String name=(String)(jsonObj.get("name"));//读取解析后的对象 jsonObj.put("initial",name.substring(0,1).toUpperCase()); //添加新的值 String[] likes=new String[]{ "JavaScript", "Skiing", "Apple Pie" }; jsonObj.put("likes",likes); Map ingredients=new HashMap(); ingredients.put("apples","3kg"); ingredients.put("sugar","1kg"); ingredients.put("pastry","2.4kg"); ingredients.put("bestEaten","outdoors"); jsonObj.put("ingredients",ingredients); %><%=jsonObj%> <!--以json的形式输出对象-->
另外我们还要用到包装集:
prototype.js
和json.js
效果如下:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
相关文章推荐
- 使用json往返传输数据 post方法
- C#中在WebClient中使用post发送数据实现方法
- 利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- jQuery实现使用sort方法对json数据排序的方法
- php获取post中的json数据的实现方法
- js实现内容显示并使用json传输数据
- 使用mock.js随机数据和使用express输出json接口的实现方法
- iOS 使用http POST方法传输二进制数据
- 使用JSON实现数据的跨域传输
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类
- 微信的API都是通过https调用实现的,分为post方法调用和get方法调用。不需要上传数据的采用get方法(使用IntraWeb开发)
- httpclient模拟post请求json封装表单数据的实现方法
- php获取post中的json数据的实现方法
- (转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- PHP中利用jQuery操作json格式数据,实现$_POST的数据传输和接收
- php获取post中的json数据的实现方法
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- 利用HttpWebRequest以POST方式提交Json数据-后台实现不同平台间的数据传输
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互