前端之家收集整理的这篇文章主要介绍了
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"/>
原文链接:https://www.f2er.com/ajax/164989.html