0

i am sending 2 data to the servlet through ajax from jsp and the servlet just get those data, then convert to json obj and return that json. now the jsp get that json to show those 2 data. ajax is successfully executing the servlet but everytime is alerts me the error not the success. pls tell me what should i do to display those data in alert? details.jsp:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>            
    function doajax(){
        $.ajax({
            url: "AccountDetails",
            type: "post",
            dataType: "json",
            data: { "mobileNo":"01914752849", "fullName":"Md. Muzahid-ul Islam" },
            error:function(){
                alert("error occured!!!");
            },
            success:function(data){
                alert(data.fullName + "\n" + data.mobileNo);
            }
         });
      }
</script>

AccountDetails.java(servlet):

PrintWriter out = response.getWriter();
try {
    String fullName = request.getParameter("fullName");
    String mobileNo = request.getParameter("mobileNo");
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("fullName", fullName);
    jsonObject.put("mobileNo", mobileNo);
    out.println(jsonObject);
} finally {
    out.flush();
    out.close();
}
  • Are you getting a error in firebug? – Jayantha Lal Sirisena May 23 '12 at 04:06
  • *firstly try to set the content type in jsp to **application/json** after that check whether you configured the web.xml for the url **AccountDetails** and then do the trick, I think it will solve the problem – Ankur Verma May 23 '12 at 04:13
  • thanks for ur reply. @jayantha, unfortunately i am not familiar with the firebug :( @ankur20us, chk out the script code, i hv specify the content type there. moreover i am using these 2 lines b4 creating the 'out' obj: `response.setContentType("application/json"); response.setCharacterEncoding("UTF-8");` – Md. Muzahid-ul Islam May 23 '12 at 04:30
  • Ahh.. can you use Chrome and look in the console – Jayantha Lal Sirisena May 23 '12 at 04:34
  • `
    ` this is my html part calling the script. interesting part is when i am debugging @ chrome at a stage of step over, its showing me the data. but when without the debugging its showing nothing (after commenting the error part) :( i am getting confused of having such problem !!!
    – Md. Muzahid-ul Islam May 23 '12 at 05:23
  • guys, do u hv any diff/other idea to get data as json from servlet? pls tell me. thanks. – Md. Muzahid-ul Islam May 23 '12 at 05:41
  • i hv changed the error msg to: `function(xhr,err) { alert('Ajax readyState: '+xhr.readyState+'\nstatus: '+xhr.status + ' ' + err); }` and now i am getting alert with: Ajax readyState: 0 and status: 0 error there is no err but the program enters the error block !!! – Md. Muzahid-ul Islam May 23 '12 at 05:57
  • well at last i hv found the solution of my own :) after replacing the entire code within the doajax function and place it in document.ready function then its working. i am sharing my exp. for those who r suffering like me. anyway thanks for ur reply and effort. – Md. Muzahid-ul Islam May 24 '12 at 03:34

1 Answers1

0

Edit code in servlet to

Gson gson = new Gson();
JsonElement element = gson.toJsonTree(<Object_to_be_passed or model_class>);
out.write(element.toString());

and also you have to import two packages com.google.gson.Gson and com.google.gson.JsonElement in servlet

Note: try to forward your data through wrapping them in a model class

Hope it helps..:)

Chirag
  • 555
  • 1
  • 5
  • 20