Solved

jquery ajax xml response not coming from jsp

Posted on 2011-02-19
7
1,516 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now