1

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

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. Here what is the significance of response.* after the is else . I want to get data from json string in else block
What is request parameter` here, response is same like request parameter=> HttpServletResponse Object
Now test the above answer I've made changes in it.
I want ouput as some mobile number as 9922xxxx but i am getting 11111/"something went wrong" ,cause i am not getting json string from jquery
Check by using console what goes to the server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.