ajax发送json,后台取得数据
package com.xeon.mis.action; import java.io.BufferedReader; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.xeon.mis.dao.StudentDao; import com.xeon.mis.model.Student; import com.xeon.mis.utils.JsonUtils; public class QueryAction extends ActionSupport { private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); BufferedReader reader = request.getReader(); StringBuffer buffer = new StringBuffer(); String str; while ((str = reader.readLine()) != null) { buffer.append(str); } if (buffer == null || "".equals(buffer.toString())) { return ERROR; } return SUCCESS; } }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ajax.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript" src="js/json.js"> </script> <script type="text/javascript"> var xhr; function send(url) { create(); var jsonuser = { major : "动漫设计" }; var str = JSON.stringify(jsonuser); if (xhr != null) { xhr.onreadystatechange = statechange; xhr.open("POST", url, true); xhr.send(str); } else { alert("浏览器不支持!!!"); } } function create() { if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject(Microsoft.XMLHTTP); } } function statechange() { if (xhr.readyState == 4 && xhr.status == 200) { document.getElementById("a").innerHTML = xhr.responseText; } } </script> </head> <body> <div id="a"> </div> 发送json <input type="button" onclick="send(‘query.action‘)" value="send" /> </body> </html>
<action name="query" class="com.xeon.mis.action.QueryAction"/>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。