?
Solved

ajax response not coming from spring action

Posted on 2011-05-07
29
Medium Priority
?
1,514 Views
Last Modified: 2013-11-08
i am executing AJAX call to spring flow action class but the response is not coming to jsp.i am getting error at the client side.
do i need to set application.json in action class.if yes how?




   sample.jsp.
    ************
     $.ajax({	
						url: "${flowExecutionUrl}&_eventId=proceed",
						dataType: "json",
						data: $('#frm').serialize()+"&ssn="+$("#ssn").val(),
						contentType: "application/json; charset=utf-8",
					    success: function(data){ 
							   alert(data.employees.length)
						         alert("success")  
								},   
							error: function(e, xhr){ 
								   alert("error");
							}
					});

flowAction.java
****************
public String  generateCode(RequestContext context) {
    	StringBuffer emps =null;
	
    	try {
    		  String name = context.getExternalContext().getRequestParameterMap().get("name");
    		  String ssn = context.getExternalContext().getRequestParameterMap().get("ssn");
    		  String zipcode = context.getExternalContext().getRequestParameterMap().get("zipcode");
    		  emps = new StringBuffer();
		  emps.append("{\"employees\":\"ABCD\"}");
		 context.getExternalContext().getResponseWriter().write(emps.toString());				
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("emps....emps........."+emps.toString());
		return "success";
    }


 

flow.xml
*********
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

<view-state id="sample" view="samle.jsp">
		<transition on="proceed" to="save"/>
	</view-state>
	
	   <action-state id="save"> 
        <evaluate expression="flowAction.generateCode(flowRequestContext)" /> 
		 <transition on="success" to="sample" /> 
        </action-state> 
</flow>

Open in new window

0
Comment
Question by:chaitu chaitu
  • 14
  • 13
  • 2
29 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35714508
check your logs to see where the problem is occurring
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35714611
in the logs method is executing but response is not coming back..
0
 
LVL 92

Expert Comment

by:objects
ID: 35714617
a response always comes back
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 35714627
try debugging your javascript to determine what is happening with the response
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35714636
do u now how to check in firefox?
0
 
LVL 92

Expert Comment

by:objects
ID: 35714642
use firebug, it will show you details of ajax requests
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35714650
i dont know where to check ajax requests in firefox?can you provide any link or explain it here?
0
 
LVL 92

Expert Comment

by:objects
ID: 35714978
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35714985
now the response is coming back but its printing whole code in alert.i am expecting only Hello to be printed in the alert but its printing whole source code in the alert.

  success: function(data){

            alert(data)
            },  
0
 
LVL 92

Expert Comment

by:objects
ID: 35715074
no that sounds right, data is everything returned by the server
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35715262
>but its printing whole code in alert

It may be an error page, because the code on the server page throw a Spring exception
You're using GET method, to see the page content put the URL of your ajax call directly in the browser address to see the content of page returned
0
 
LVL 92

Expert Comment

by:objects
ID: 35715269
you can also view the content of the response in firebug
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35715389
Please find the attached screen shots for xml response .

"It may be an error page, because the code on the server page throw a Spring exception
You're using GET method, to see the page content put the URL of your ajax call directly in the browser address to see the content of page returned "

page is coming when i hit the url directly .any how in spring action class i put printStacktrace if any error comes it will print the error.now my doubt is with the flow action code.

to confirm my doubt I tried in another way like calling one jsp to another jsp using ajax call at that time response is coming correctly but when put spring action class in the middle then i am getting HTML code as response.SO may be problem lies in the spring action class.

String name = context.getExternalContext().getRequestParameterMap().get("name");
    		  String ssn = context.getExternalContext().getRequestParameterMap().get("ssn");
    		  String zipcode = context.getExternalContext().getRequestParameterMap().get("zipcode");
    		  emps = new StringBuffer();
		  emps.append("{\"employees\":\"ABCD\"}");
		 context.getExternalContext().getResponseWriter().write(emps.toString());

Open in new window

                       
      
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35715392
screen shot attached..
ajaxrespose.docx
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35715431
I see a :  302 Moved temporarily
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35715455
does it mean is there any error in action class??
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35715457
i think writing to response object in spring may be  a problem.am still finding out.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 35716977
looks like you are making an ajax request for the actual jsp page
0
 
LVL 92

Expert Comment

by:objects
ID: 35717005
check the url (thru firebug) that your ajax is sending, and check what it is mapped to one on the server
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35744518
objects/leakim,

"looks like you are making an ajax request for the actual jsp page "

in your previous comment .this is what you are saying.see below.i think this is where i am not getting response.

when the page index page loads initially the sample jsp page will be displayed.when user clicks proceed button then i made a AJAX call
to the action class(flowAction) and calls the method (generateCode) if it is succcess here in this case i am redirecting to the same jsp page i.e. sample.jsp.

my doubt is do i need to redirect to different jsp page to get the response from AJAX?

if yes what should i put in that page?


flow xml .
*************
<start-state idref="sample" /> 

<view-state id="sample" view="samle.jsp">
		<transition on="proceed" to="save"/>
	</view-state>
	
	   <action-state id="save"> 
        <evaluate expression="flowAction.generateCode(flowRequestContext)" /> 
		 <transition on="success" to="sample" /> 
        </action-state> 
</flow>

flowAction
************

public String  generateCode(RequestContext context) {
    	StringBuffer emps =null;
	
    	try {
    		  String name = context.getExternalContext().getRequestParameterMap().get("name");
    		  String ssn = context.getExternalContext().getRequestParameterMap().get("ssn");
    		  String zipcode = context.getExternalContext().getRequestParameterMap().get("zipcode");
    		  emps = new StringBuffer();
		  emps.append("{\"employees\":\"ABCD\"}");
		 context.getExternalContext().getResponseWriter().write(emps.toString());				
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		System.out.println("emps....emps........."+emps.toString());
		return "success";
    }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35744806
> my doubt is do i need to redirect to different jsp page to get the response from AJAX?

yes

> if yes what should i put in that page?

whatever it is you expect to get back from the request
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35744880
so i need to put below code in the different jsp.i want to see employees as response when ajax call is over..

<%
StringBuffer  emps = new StringBuffer();
              emps.append("{\"employees\":\"ABCD\"}");
             response.getWriter().write(emps.toString());
%>      
0
 
LVL 92

Expert Comment

by:objects
ID: 35744927
it can just be:

{"employees":"ABCD"}
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35744950
so i will remove above code in action class because i am doing it here in jsp.

as a example i have hardhoded .if i need pass the object from action class to this jsp.

what kind of scopes you wil use to get the object from action to jsp??
0
 
LVL 92

Expert Comment

by:objects
ID: 35744970
request scope
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35744989
can you provide some links how tp put these scopes
0
 
LVL 92

Expert Comment

by:objects
ID: 35745042
context.getRequestScope().put(key, value);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 35745126
can i retrive in jsp like request.getAttribute("key");
0
 
LVL 92

Expert Comment

by:objects
ID: 35751402
yes I believe so
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 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