|
原意想法是这样:
客户端用JS生成JSON数据,通过Ajax框架prototype发放到服务端;服务端接收JSON数据后利用JSON in java重新组织成JSON对象,再生成JSON字符串回调Ajax客户端函数;在被回调的函数中显示数据。
客户端代码如下:
sendData.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
<head>
<script src="json.js"></script>
<script src="prototype.js"></script>
<title>使用JSON示例</title>
</head>
<body>
<script language="javascript">
function sendData(){
usersVar={ "userName": "jj", "address":{"province":"123","city":"cs"}, "email": "xiangxiangd126.com" };
var url="dealData.jsp?timeStamp="+new Date().getTime();
var myAjax=new Ajax.Request(url,{
method:'post',
parameters:usersVar,
requestHeaders:{Accept:'application/json'},
contentType: 'application/x-www-form-urlencoded',
onComplete:showResponse,
asynchronous:true
});
}
sendData();
function showResponse(originalRequest){
var user=originalRequest.responseText.evalJSON();
document.write("JSON是:");
document.write(user.email);
}
</script>
</body>
</html>
服务端代码如下:
dealData.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ page import="org.json.*,java.util.HashMap" %>
<html>
<head>
<script src="json.js"></script>
<script src="prototype.js"></script>
<title>使用JSON示例</title>
</head>
<body>
<%
String userName=request.getParameter("userName");
String address=request.getParameter("address");
String email=request.getParameter("email");
HashMap<String,String> map=new HashMap<String,String>();
map.put("userName",userName);
map.put("address",address);
map.put("email",email);
JSONObject userJsonObj=new JSONObject(map);
out.print(userJsonObj.toString());
%>
</body>
</html>
结果发给用浏览器访问客户端页面时,页面无任何显示,也无任何报错。请问是什么问题? |
|