1 Replies - 2114 Views - Last Post: 02 September 2013 - 11:38 AM

#1 rishabhsharma  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 342
  • Joined: 26-March 09

Can't Read JSON Value Sent From Servlet In JQuery

Posted 28 August 2013 - 02:29 AM

I am building a page where users are able to edit their account. Coming directly to my question, I am using AJAX to read JSon value sent from Servlet side, but when I am trying to run commands like alert(data.Text) or alert(data), it is either doing nothing or alerts 'null'. If I remove alert() invocation; everything is running fine, except, I can't read responseText. I want to use it for purpose like comparison. For example: if(data.Text == "Success") then do something.

Snippet 1: Ajax
function saveName()
			 {
			   var fname = document.getElementById("fname").value;
			   var lname = document.getElementById("lname").value;
			   $.ajax({
						type: "Post",
						url : "http://localhost:8080/examples/accountupdate",
						data: {fname:fname,lname:lname},
						datatype: 'json',
						success:function(data)
							{
								   alert(data.Text);//If I remove this line then ajax is performing what it is supposed to
								   var full = fname+" "+lname ;
								   document.getElementById("field_value").innerHTML=full;
								   $("#field_value").slideDown(50) ;
								   $("#fullname_edit").slideDown(50);
								   $("#fullname_division").slideUp(200);
								   setNewName(fname) ;
								   document.getElementById("loggedpeoplename").innerHTML=fname;
							},
						error: function(responseText)
						  {
							alert("Some Error") ;
						  }
					});
			 }



Snippet 2: Servlet

response.setContentType("application/json;charset=UTF-8");
					request.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
                    con = (Connection)getServletContext().getAttribute("connection") ;
					stat1 = con.createStatement() ;
					String fname = (String)request.getParameter("fname") ;
					String lname = (String)request.getParameter("lname") ;
					HttpSession session =request.getSession() ;
					String uniqueid = (String)session.getAttribute("UniqueId") ;
					int x = stat1.executeUpdate("update member set fname='"+fname+"',lname='"+lname+"' where uniqueid='"+uniqueid+"'") ;
					JSONObject jsonResponse = new JSONObject();
						jsonResponse.put("Text", "Success");
					JSONObject.toJSONString(jsonResponse);
					out.flush();
					out.close();



what's wrong with my code.

This post has been edited by rishabhsharma: 28 August 2013 - 02:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't Read JSON Value Sent From Servlet In JQuery

#2 Ulrik  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 7
  • Joined: 02-September 13

Re: Can't Read JSON Value Sent From Servlet In JQuery

Posted 02 September 2013 - 11:38 AM

Try doing

alert(JSON.parse(data).Text);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1