Solved

jquery ajax xml response not coming from jsp

Posted on 2011-02-19
7
1,521 Views
Last Modified: 2012-05-11
     i am calling jsp through jquery ajax method.but the response is not coming.how to get the response which is in xml format?i put log in jsp there value is printing.but the xml response is not coming .

please modify the code if i written wrongly..
                              
                              $.ajax({      
                                    url: "calculate.jsp",
                                    type:"POST",
                                     dataType: "xml",
                                    data:$('#'+frmid).serialize()+'&requestType=AJAX',
                                    success: function(data) {
                                          obj.complete(data);
                                    },
                           
                              });

                                    complete:function(data){
                                          alert($(data))
                                    var firstname = $(data).find("firstname")[0].nodeValue;
                  },


calculate.jsp
*************
String      suppLastName1 = request.getParameter("suppLastName1");
String      a1firstNameTmp = request.getParameter("a1firstNameTmp");
System.out.println("suppLastName1....."+suppLastName1);
       response.setContentType("text/xml");
          response.setHeader("Cache-Control", "no-cache");
          response.getWriter().write("<application><firstname>"+a1firstNameTmp+"</<firstname><lastname>"+suppLastName1+"</<lastname></application>");
0
Comment
Question by:chaitu chaitu
  • 4
  • 3
7 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34933443
In bold :
+"</<firstname>
+"</<lastname>

corrected :


response.getWriter().write("<application><firstname>"+a1firstNameTmp+"</firstname><lastname>"+suppLastName1+"</lastname></application>");

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34935061
work for me :

<%@ page contentType="text/xml" %>
<%@ page import="java.sql.*" %>
<%
	String suppLastName1 = request.getParameter("suppLastName1");
	String a1firstNameTmp = request.getParameter("a1firstNameTmp");
	response.setContentType("text/xml");
	response.setHeader("Cache-Control", "no-cache");
	response.getWriter().write("<application><firstname>"+a1firstNameTmp+"</firstname><lastname>"+suppLastName1+"</lastname></application>");
%>

Open in new window

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans nom</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("input[type='button']").click(function() {
			var frmid = "myform";
			$.ajax({      
				url: "calculate.jsp",
				type:"POST",
				dataType: "xml",
				data:$('#'+frmid).serialize()+'&requestType=AJAX',
				success: function(data) {
					firstname = $("firstname", data).text();
					alert(firstname)
				},		
			});
		});
	});
</script>
</head>
<body>
<input type="button" value="load" />
<form id="myform">
	<input type="text" name="suppLastName1" value="Experts" />
	<input type="text" name="a1firstNameTmp" value="Exchange" />
</form>
</body>
</html>

Open in new window

0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 34940170
i have one clarification regarding normal reloading the page and ajax call.

whatever values entered in the form cannot be re-captured if i click load button in the case of Reload.jsp but if we replace with ajax code and click load button then previously entered values recaptured in the form with re assigning back to form values.

success: function(data) {
                              firstname = $("firstname", data).text();
                              lastname = $("lastname", data).text();
                              alert(firstname)

                  //do i need to re assign like this back to form values or automaticlaly will be re assigned.
                        
                  $("input[name='a1firstNameTmp']").value=firstname
                  $("input[name='suppLastName1']").value=lastname
},



Reload.jsp
***********

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans nom</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script language="javascript">
      $(document).ready(function() {
            $("input[type='button']").click(function() {
                  

                  location.href="Reload.jsp"

            });
      });
</script>
</head>
<body>
<input type="button" value="load" />
<form id="myform">
      <input type="text" name="suppLastName1" value="Experts" />
      <input type="text" name="a1firstNameTmp" value="Exchange" />
</form>
</body>
</html>

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Expert Comment

by:leakim971
ID: 34940184
?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 34940296
you didnt understand my doubt??
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 34948514
my requirement is it has goto server side once when the session is time out so whatever values entered before session time out should be captured and to reassigned back to form values.

  //do i need to re assign like this back to form values or automaticlaly will be re assigned.
                       
                  $("input[name='a1firstNameTmp']").value=firstname
                  $("input[name='suppLastName1']").value=lastname
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34948600
Just to be sure what you recorded is same as what you're displaying I would choose to re assign
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now