ajax response not coming from spring action

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

LVL 20
chaitu chaituAsked:
Who is Participating?
 
objectsCommented:
looks like you are making an ajax request for the actual jsp page
0
 
objectsCommented:
check your logs to see where the problem is occurring
0
 
chaitu chaituAuthor Commented:
in the logs method is executing but response is not coming back..
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
objectsCommented:
a response always comes back
0
 
objectsCommented:
try debugging your javascript to determine what is happening with the response
0
 
chaitu chaituAuthor Commented:
do u now how to check in firefox?
0
 
objectsCommented:
use firebug, it will show you details of ajax requests
0
 
chaitu chaituAuthor Commented:
i dont know where to check ajax requests in firefox?can you provide any link or explain it here?
0
 
objectsCommented:
0
 
chaitu chaituAuthor Commented:
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
 
objectsCommented:
no that sounds right, data is everything returned by the server
0
 
leakim971PluritechnicianCommented:
>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
 
objectsCommented:
you can also view the content of the response in firebug
0
 
chaitu chaituAuthor Commented:
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
 
chaitu chaituAuthor Commented:
screen shot attached..
ajaxrespose.docx
0
 
leakim971PluritechnicianCommented:
I see a :  302 Moved temporarily
0
 
chaitu chaituAuthor Commented:
does it mean is there any error in action class??
0
 
chaitu chaituAuthor Commented:
i think writing to response object in spring may be  a problem.am still finding out.
0
 
objectsCommented:
check the url (thru firebug) that your ajax is sending, and check what it is mapped to one on the server
0
 
chaitu chaituAuthor Commented:
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
 
objectsCommented:
> 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
 
chaitu chaituAuthor Commented:
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
 
objectsCommented:
it can just be:

{"employees":"ABCD"}
0
 
chaitu chaituAuthor Commented:
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
 
objectsCommented:
request scope
0
 
chaitu chaituAuthor Commented:
can you provide some links how tp put these scopes
0
 
objectsCommented:
context.getRequestScope().put(key, value);
0
 
chaitu chaituAuthor Commented:
can i retrive in jsp like request.getAttribute("key");
0
 
objectsCommented:
yes I believe so
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.