JQuery 发送Json格式数据
简述:
需要封装一组Json格式的数据到服务器
知识点:
1.调用jquery.json的库
2.AJAXPost请求
Jquery下载地址
http://jquery.com/download/
Jquery.json
http://code.google.com/p/jquery-json/
<%@ page contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <c:set var="ctx" value="${pageContext.request.contextPath}" /> <html> <head> <title>发送Json格式数据</title> <script type="text/javascript" src="./lib/js/jquery-1.8.3.js" ></script> <script type="text/javascript" src="./lib/js/jquery.json-2.4.min.js"></script> <script type="text/javascript"> function sendJson() { var requestStr = { reqStr: 'test parameter' }; var request = { requestId: 'asdfasdfa', sessionId: 'asdfasdfasdf', userName: 'Jeremy', password:'123', request: requestStr }; //调用了jquery.json 库 var encoded = $.toJSON( request ); var jsonStr = encoded; var actionStr = $("#actionPath").val(); $.ajax({ url : "${ctx}/" + actionStr, type : 'POST', data : jsonStr, dataType : 'json', //contentType : 'application/json', success : function(data, status, xhr) { // Do Anything After get Return data // $.each(data.payload, function(index){ // $("#result").append("</br>" + data.payload[index].beanStr); // }); }, Error : function(xhr, error, exception) { // handle the error. alert(exception.toString()); } }); } </script> </head> <body> <!-- Send JSON --> <B>Action Path: </B><input id="actionPath" type="text" style="width: 300px;"/> <button class="button" id="buttonUpload" style="margin-left:10px;" onclick="return sendJson();">Send</button> <div id="result" >result: </div> </body> </html>
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17