JQuery 发送Json格式数据

http://blog.csdn.net/anialy/article/details/8591651

简述:

需要封装一组Json格式的数据到服务器


知识点:

1. 调用jquery.json的库

2. AJAX Post 请求


Jquery下载地址

http://jquery.com/download/


Jquery.json

http://code.google.com/p/jquery-json/


代码:

testSendJson.jsp

[html]  view plain copy
  1. <%@ page contentType="text/html;charset=UTF-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <c:set var="ctx" value="${pageContext.request.contextPath}" />  
  4.   
  5. <html>  
  6. <head>  
  7. <title>发送Json格式数据</title>  
  8.   
  9. <script type="text/javascript" src="./lib/js/jquery-1.8.3.js" ></script>  
  10. <script type="text/javascript" src="./lib/js/jquery.json-2.4.min.js"></script>  
  11.   
  12. <script type="text/javascript">  
  13. function sendJson() {  
  14.     var requestStr = {  
  15.             reqStr:  'test parameter'  
  16.     };  
  17.     var request = {   
  18.             requestId: 'asdfasdfa',  
  19.             sessionId: 'asdfasdfasdf',  
  20.             userName: 'Jeremy',  
  21.             password:'123',  
  22.             request: requestStr  
  23.     };  
  24.     //调用了jquery.json 库  
  25.     var encoded = $.toJSON( request );  
  26.     var jsonStr = encoded;  
  27.     var actionStr = $("#actionPath").val();  
  28.     $.ajax({  
  29.         url : "${ctx}/" + actionStr,  
  30.         type : 'POST',  
  31.         data : jsonStr,  
  32.         dataType : 'json',  
  33.         //contentType : 'application/json',  
  34.         success : function(data, status, xhr) {  
  35. //         Do Anything After get Return data  
  36. //          $.each(data.payload, function(index){  
  37. //              $("#result").append("</br>" + data.payload[index].beanStr);  
  38. //          });  
  39.         },  
  40.         Error : function(xhr, error, exception) {  
  41.             // handle the error.    
  42.             alert(exception.toString());  
  43.         }  
  44.     });  
  45. }  
  46. </script>  
  47. </head>  
  48. <body>  
  49.     <!-- Send JSON -->  
  50.     <B>Action Path:   </B><input id="actionPath"  type="text"  style="width: 300px;"/>  
  51.     <button class="button" id="buttonUpload" style="margin-left:10px;"  
  52.                 onclick="return sendJson();">Send</button>  
  53.     <div id="result" >result: </div>  
  54. </body>  
  55. </html>  


输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值