I am making a YUI AJAX call from my JSP as
var oConnect = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
The URL specified is
var sUrl = "http://localhost:8080/Test/TreeViewData.do";
The call is going to the TreeViewAction.java , based on entries in struts-config.xml and the following is getting executed-
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Action");
JSONObject company = new JSONObject();
company.put("name","Software Inc.");
JSONArray employees = new JSONArray();
JSONObject employee1 = new JSONObject();
employee1.put("name","Mike");
employee1.put("skill","Java");
employees.put(employee1);
JSONObject employee2 = new JSONObject();
employee2.put("name"," Joe");
employee2.put("skill","HTML");
employees.put(employee2);
company.put("employees",employees);
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().print(company.toString());
//Even tried this //company.write(response.getWriter());
return (mapping.findForward(Constants.SUCCESS)); }
On succes it returns to the main JSP, but the response received is not in JSON format and not getting Parsed using -
try {
var theResponse = YAHOO.lang.JSON.parse(oResponse.responseText);
} catch (e)
{ alert(e.message);
}
It gives JASON.parse exception as message.
When I do alert(oResponse.getAllResponseHeaders),I get-
Cache-control:no-cache
Content-Type:text/html;charset:UTF-8 T
ransfer-Encoding: chunked
Date:Fri 10 dec
Server:Apache-Coyote/1.1
and on alert(oResponse.responseText); I get
<link rel="stylesheet" type="text/css" href="../../build/fonts/fonts-min.css" /> <link rel="stylesheet" type="text/css" href="../../build/treeview/assets/skins/sam/treeview.css" /> <script type="text/javascript" src="jscripts/yuiloader/yuiloader.js"></script> <script type="text/javascript" src="jscripts/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="jscripts/treeview/treeview-min.js"></script> ....and so on....
Could someone please help get his Jason data in JSP, I am trying to implement a YUI treeview which will take the data from javaaction class(not PHP) and create a dynamic tree.