Reputation: 2385
I am developing web application.
I am sending json string from one page to another.
I have used jquery to send the post data.
In the target page I want to retrieve the same data.
But I am getting the null
value there.
my page which posts json string to page is:
$(document).ready(function () {
$.post("./ProfileUser.jsp",{jsonData:jsonstr});
$(location).attr('href',"./ProfileUser.jsp");
});
And in the page 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");
}
%>
I am getting output as Something went wrong
which should be mobile number from the database.
How should I get the json data in jsp?
Thank you
Upvotes: 1
Views: 575
Reputation: 40639
First you need to add the third parameter
as json in $.post()
like,
$.post("./ProfileUser.jsp",{jsonData:jsonstr},'json');
Secondly, in JSP try it like,
JSONObject json = new JSONObject();
if(!jsonData) {
json.put("mobile", "111111");
}
else{
//something;
}
response.setContentType("application/json");
response.getWriter().write(json.toString());
Upvotes: 1