ajax请求action返回json数据

1,服务端:

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
JSONObject json = new JSONObject();
json.put("filename", filename);
json.put("dataStr", dataStr);
try {
response.getWriter().print(json.toString());
response.getWriter().close();
} catch (IOException e) {
log.fatal(e);
}

2,客户端:

$.ajax({
url : "*.action",
contentType: "application/json; charset=utf-8",
data : "data="data,
dataType:"json",
async : false,
success : function(data) {
//var dataJson = data.parseJSON;
var dataString = data["dataStr"];
var fileName = data["filename"];
exportCsv2(dataString, fileName);//导出
location.reload();//重新加载页面
},
error : function(){
alert("error");
}
});
上一篇:ORACLE 连接SQLSERVER 数据库备忘


下一篇:ajax对服务端发送请求