I have a simple script, that fills html form with values recieved from server
$.ajax({
url: getUrl(),
dataType: "xml"
}).done(function(xml) {
//get values from xml file and fill in html form
});
and simple code on a server side, that generates xml
public void writeXmlValues() throws Exception {
final HttpServletResponse response = context.getHttpResponse();
response.setContentType("application/xml; charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
writeXmlValues(out, values);
}
finally
{
try
{
out.close();
}
catch (Throwable ignored){}
}
}
I faced the problem when added textarea field in form. This field can contain text with line separators and I'm able to put multiline text in xml. The result will be as follows:
<?xml version='1.0' encoding='UTF-8'?>
<parameter name="Conclusion">eshgseht
awegewhw
agwhg</parameter>
But text in js callback looks little different (here is the code exactly as it IS):
<?xml version='1.0' encoding='UTF-8'?><parameter name="Conclusion">eshgseht awegewhw agwhg</parameter>
There is no line feeds anymore! This is not a fault of jquery (as far as I can see in Firebug, response already is an one-line text), this is not a fault of StAX (it generates proper xml). I have no idea what's going on. Can I get line feeds with usage of XML format? Or I should use json or something else (this is not the preferred way)?
Update: Finally I found the reason for this behavior. Response was interpreted as text/html even with setContentType("application/xml"). Thanks a lot for your suggestions.