Solved

jquery ajax xml response not coming from jsp

Posted on 2011-02-19
7
1,532 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

860 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