首页 > Web开发 > 详细

ajax发送json,后台取得数据

时间:2014-04-19 23:28:55      阅读:724      评论:0      收藏:0      [点我收藏+]
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"/>



ajax发送json,后台取得数据,布布扣,bubuko.com

ajax发送json,后台取得数据

原文:http://blog.csdn.net/hackcoder/article/details/24131683

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!