我正在开发web应用程序。
我正在将json字符串从一个页面发送到另一个页面。我已经使用jquery发送了post数据。在目标页面中,我希望检索相同的数据。但是我在那里得到了null的值。
我将json字符串发布到页面的页面是:
$(document).ready(function () {
$.post("./ProfileUser.jsp",{jsonData:jsonstr});
$(location).attr('href',"./ProfileUser.jsp");
}); 在页面ProfileUser.jsp中
<%
String jsonData = request.getParameter("jsonData");
String mobile;
if(jsonData == null) mobile = "something went wrong";
else {
JSONObject j =new JSONObject(jsonData);
mobile = j.getString("mobile");
}
%>我得到的输出作为Something went wrong,这应该是从数据库的手机号码。
如何获取jsp中的json数据?
谢谢
发布于 2013-08-05 13:31:10
First你需要在$.post()中添加third parameter作为json,就像
$.post("./ProfileUser.jsp",{jsonData:jsonstr},'json');其次,中的尝试一下,
JSONObject json = new JSONObject();
if(!jsonData) {
json.put("mobile", "111111");
}
else{
//something;
}
response.setContentType("application/json");
response.getWriter().write(json.toString());https://stackoverflow.com/questions/18050914
复制相似问题