Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

AJAX response contains original page!?!

Hi there,
I'm hoping someone can help me out here. I've only just started with AJAX and am a little lost. I'm writing a JSP application, using Spring MVC, and AJAX.

I have a JSP which is initially rendering, then on a user action, is posting a request back to my control servlet using AJAX. I then do the following,

response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<message>test</message>");
response.getWriter().flush();

The problem I'm finding is, that back in my Javascript of the JSP, when I'm inspecting the returned XMLHttpRequest, the responseText contains the source HTML from the originally rendered page. My piece of XML is pre-appended to this. However, due to the strange nature of the returned text, the responseXML is showing parseerror.

In the examples I've found online, often the response buffer isn't flushed - however, when I've tried this, my new XML doesn't appear at all on the response - either in the responseText or responseXML.

Please can someone save me a lot of head scratching? Thanks in advance.
[1279 byte] By [Java_Maniac] at [2007-11-11 10:05:36]
# 1 Re: AJAX response contains original page!?!
Well, I'm answering my own question. Some people say that talking to yourself is the first sign of madness. Who said that? Oh it was you. :P

The answer to this problem is the way I was returning an uncleared ModelAndView from the Spring controller object. Instead do something like this, for the perfect result;-

ModelAndView dummy = new ModelAndView();
dummy.clear();
return dummy;
Java_Maniac at 2007-11-11 23:42:26 >