View Full Version : Ajax Call with Spring Contoller fails.
amitr2006
Mar 21st, 2006, 08:02 PM
Hi,
I am a newbie with Spring. I am trying to integrate Ajax request with Spring. I am doing that by returning null ModelAndView object from my controller. Meanwhile I have set the request object content type and write the xml data on the responsewriter. But I do not have any success.
Please help if there is anything that I have missed out.
>>>>>>
response.setContentType("text/xml");
response.getWriter().write(strXmlDoc);
return null;
<<<<<<
On the second note, I want to ask is this the only method or Do I have any other ways like implementing AbstractViews to use with AJAX
Please help...
russpitre
Mar 21st, 2006, 09:49 PM
Have you thought about using DWR?
http://getahead.ltd.uk/dwr/
Colin Yates
Mar 22nd, 2006, 04:56 AM
I am a newbie with Spring. I am trying to integrate Ajax request with Spring. I am doing that by returning null ModelAndView object from my controller. Meanwhile I have set the request object content type and write the xml data on the responsewriter. But I do not have any success.
Please help if there is anything that I have missed out.
The first thing to understand is that AJAX is purely a client side concept. To the server it makes no difference whether the request is from a web browser or an "AJAX client" (which is just the web browser). Of course you need to ensure that you return information that the AJAX client understands...
On the second note, I want to ask is this the only method or Do I have any other ways like implementing AbstractViews to use with AJAX
Sure you can use Springs View hierarchy, but you can also do as you are doing (writing to the response and returning null).
What exactly is failing, are you not seeing any response? What happens if you go directly to the url in the browser?
tobysaville
Mar 22nd, 2006, 06:41 AM
could it be your controller requires a POST request, but your submitting a GET request?
try setting your controller as the action of a form with method="post"
amitr2006
Mar 22nd, 2006, 10:01 AM
Thanks All for responses.
I have been able to get the call at controller but I am unable to send the
xml <<some default xml content>> back to javascript code.
Here is the client code
>>
req.onreadystatechange = getReadyStateHandler(req, addFilterLevels);
<<
>>>
function addFilterLevels(filterLevels) {
alert('calling addFilters > '+filterLevels);
}
<<<
>>>>
function getReadyStateHandler(req, responseXmlHandler) {
// Return an anonymous function that listens to the XMLHttpRequest instance
return function () {
// If the request's status is "complete"
if (req.readyState == 4) {
// Check that we received a successful response from the server
if (req.status == 200) {
// Pass the XML payload of the response to the handler function.
alert ('>> ' + req);
alert ('>> ' + req.responseXML);
responseXmlHandler(req.responseXML);
} else {
// An HTTP problem has occurred
alert("HTTP error "+req.status+": "+req.statusText);
}
}
}
<<<<
Below is the log. That describes the request is going to controller. But thereafter
cookie: JSESSIONID=071B6DE8144C095D2D63363214541428; /springapp/navBar.htm=sc1|
2006-03-22 09:57:54,584 INFO [spring.SpringAppController] - <loadLevelForHierarchy > loadLevel, ETH3, 1, 14, 14>
2006-03-22 09:57:54,584 INFO [spring.SpringAppController] - <SpringAppController - returning view frm loadLevelForHierar
chy>
2006-03-22 09:57:54,600 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Null ModelAndView returned to Dispa
tcherServlet with name 'springapp': assuming HandlerAdapter completed request handling>
2006-03-22 09:57:54,600 DEBUG [org.springframework.web.servlet.DispatcherServlet] - <Successfully completed request>
2006-03-22 09:57:54,600 DEBUG [org.springframework.web.context.support.XmlWebAppl icationContext] - <Publishing event in
context [WebApplicationContext for namespace 'springapp-servlet']: RequestHandledEvent: url=[/springapp/ajax.htm]; time=
[16ms]; client=[10.10.121.59]; method=[POST]; servlet=[springapp]; session=[071B6DE8144C095D2D63363214541428]; user=[nul
l]; status=[OK]>
This gives me idea that I cannot read request object as it is blank. So just wodnering where is my response got returned to.
-Amit
Colin Yates
Mar 22nd, 2006, 10:12 AM
Whilst trouble shooting; forget about AJAX, just enter the URL straight into the browser URL|address bar and see what comes back.
Can you confirm the correct controller is being called and is returning the correct information, contentType etc.
amitr2006
Mar 22nd, 2006, 11:04 AM
On browser I received
springapp org.springframework.web.servlet.DispatcherServlet 1
meaning I have lost my response object
-Amit
Colin Yates
Mar 22nd, 2006, 11:39 AM
On browser I received
springapp org.springframework.web.servlet.DispatcherServlet 1
meaning I have lost my response object
-Amit
Eh? :)
What URL are you entering, and exactly what is being sent back (can you post the rendered source).
amitr2006
Mar 22nd, 2006, 01:02 PM
Thanks Yat,
I think I got the idea actually earlier I sent the HARDCODED xml web descriptor and thats the same being received on the browser. I verified it by using other xml docs now and it works on browsers. So this piece works fine.
But when I try to extract the response in javascript I still have the same issue (i.e. req.responseXML is coming back to be blank)
amitr2006
Mar 22nd, 2006, 01:07 PM
Just to add to last post. I tried to run url with firefox. It too returned the xml document. But when I tried it with javascript code run into null XMLHttpRequest object.
-Amit
amitr2006
Mar 22nd, 2006, 01:17 PM
I got it. After debugging javacript code, I got the req.responseText object with what I sent from the server.
Now I think I can work it from here on.
Thanks to everyone and yatesco in the group to get me though this.
-Amit
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.