2013-08-05 52 views
0

我正在开发Web应用程序。
我从一个页面发送json字符串到另一个页面。 我已经使用jquery发送发布数据。 在目标页面中,我想检索相同的数据。 但我在那里得到null值。request.parameter(String)从jquery调用时返回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数据?

谢谢

回答

1

首先您需要将third parameter添加为JSON在$.post() 一样,

$.post("./ProfileUser.jsp",{jsonData:jsonstr},'json'); 

其次,JSP尝试像,

JSONObject json  = new JSONObject(); 
if(!jsonData) { 
    json.put("mobile", "111111"); 
} 
else{ 
    //something; 
} 
response.setContentType("application/json"); 
response.getWriter().write(json.toString()); 
+0

谢谢为你的答案。这里有什么是响应的意义。我想在else块中获取json字符串的数据 –

+0

这里'request'参数是什么''response''' request parameter' =>'HttpServletResponse Object' –

+0

我已经试过了,正如你所说的,但问题存在 –

相关问题