Solved

jquery ajax xml response not coming from jsp

Posted on 2011-02-19
7
1,527 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…

803 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