AJAX and XML Response
If you want to return XML as a response to AJAX request, there are few things which needs to be taken care of:
1) Set the content type of the response as xml:
response.setContentType("application/xml");
Otherwise function xmlHttpRequest.responseXML would return a null object and the XML you are trying to return as response would be returned as String object rather than a XML object and you can access that by xmlHttpRequest.responseText.
2) Construct a well-formed XML which you are going to return in response, make the XML as standalone :
returnXML = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<root>
<allxmlelements></allxmlelements>
</root>";
Important: If the XML constructed above is not well formed you won't be able get the correct response back when you try to access the returned XML in JavaScript using:
var rspXml = xmlHttpRequest.responseXML;
Q: How to check if the XML you are returning in AJAX response is correct/well-formed or not?
A: Best way to check whether the returned XML is well formed or not is to type the URL being passed in the AJAX request directly into the browser. If the response XML is well formed then browser would display the complete XML or else it would display error in the XML. (This is the best and easiest way to check the response.)
Example: in an STRUTS application when the AJAX request is processed in an Action class you can return the XML using code below;
response.setContentType("application/xml");
response.getWriter().write(returnXML);
return (null);
Note: In a STRUTS application for an AJAX request to complete successfully, the execute method of the Action class being called should return "null" instead of an ActionForward.
This article has now moved to my new BLOG.
1) Set the content type of the response as xml:
response.setContentType("application/xml");
Otherwise function xmlHttpRequest.responseXML would return a null object and the XML you are trying to return as response would be returned as String object rather than a XML object and you can access that by xmlHttpRequest.responseText.
2) Construct a well-formed XML which you are going to return in response, make the XML as standalone :
returnXML = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<root>
<allxmlelements></allxmlelements>
</root>";
Important: If the XML constructed above is not well formed you won't be able get the correct response back when you try to access the returned XML in JavaScript using:
var rspXml = xmlHttpRequest.responseXML;
Q: How to check if the XML you are returning in AJAX response is correct/well-formed or not?
A: Best way to check whether the returned XML is well formed or not is to type the URL being passed in the AJAX request directly into the browser. If the response XML is well formed then browser would display the complete XML or else it would display error in the XML. (This is the best and easiest way to check the response.)
Example: in an STRUTS application when the AJAX request is processed in an Action class you can return the XML using code below;
response.setContentType("application/xml");
response.getWriter().write(returnXML);
return (null);
Note: In a STRUTS application for an AJAX request to complete successfully, the execute method of the Action class being called should return "null" instead of an ActionForward.
This article has now moved to my new BLOG.
Labels: AJAX, HTTP, J2EE, Java, reponse, request, response.getWriter().write, response.setContentType, responseText, responseXML, Struts, XMLHttpRequest
1 Comments:
Thanks.............
By Anonymous, at December 30, 2010 12:59 AM
Post a Comment
<< Home