-
1 Apr 2007 10:48 PM #1
How to get data from server?
How to get data from server?
I have read many JSON examples which are all static data. Now I want to get dynamic JSON data from server( use Struts), I don't know how to get it in js. Here is my code.
/*TestAction.java*/
/*results.js*/Code:public class TestAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException { try { List progInfos = new ArrayList(); ProgramInfo info = new ProgramInfo(); info.setName("YUI get data test"); info.setContent("testtesttest."); progInfos.add(info); JSONArray jsonProgInfos = JSONArray.fromObject(progInfos); request.setAttribute("jsonProgInfos", jsonProgInfos.toString()); return mapping.findForward("main"); } catch (Exception e) { logger.error(e); } return null; } }
Anyone can help me? Thanks.Code:Ext.onReady(function() { var testResults, ds, columnModel, resultsGrid; // I don't know how to access data from server, the following sentence seems make no sense testResults = eval(<!--(String)request.getAttribute("jsonProgInfos")-->); ds = new Ext.data.Store({ proxy: new Ext.data.MemoryProxy(testResults), reader: new Ext.data.ArrayReader({id: 0}, [ {name: 'name'}, {name: 'content'} ]) }); ds.load(); columnModel = new Ext.grid.ColumnModel([{ header: "Name", dataIndex: 'name', width: 100 }, { header: "Content", dataIndex: 'content', width: 400, align: 'right' } ]); resultsGrid = new Ext.grid.Grid('test-grid', { ds: ds, cm:columnModel }); resultsGrid.render(); });
-
2 Apr 2007 12:22 AM #2
You need to read stuff instead of asking.
http://www-128.ibm.com/developerworks/library/j-ajax1/
http://adaptivepath.com/publications...ves/000385.php
http://www-128.ibm.com/developerwork...Mastering+Ajax (start at part 1 and go to part 10)Founder of the Path of Exticism
-
2 Apr 2007 6:58 AM #3
MemoryProxy is used to load data that's hard-coded in a JS var. Use HttpProxy to make a request to the server for data and process the response object. Look at the examples.
-
2 Apr 2007 9:00 AM #4
Here is a Java example of generating some JSON
the fields "id", "Title", "Author", "totalcount" and "topics" have to align with the field definitions in the JSONReader object created in your JS code *AND* the fields have to be aligned (maybe transformed) into the columns in your JS code.Code:JSONObject object = new JSONObject(); object.put("id","1"); object.put("Title","Freds JSON Example"); object.put("Author","ME"); JSONArray data = new JSONArray(); data.put(object); JSONObject result = new JSONObject(); result.put("totalCount", "20"); result.put("topics",data);
A good way to test the data is to access the data URL and make sure it has the right format (for example http://www.yui-ext.com/deploy/ext-1....get-nodes.php). You can also set breakpoints in the JSONReader.js code and watch what happens to the data as it comes back.
-
2 Apr 2007 6:35 PM #5
Thank you all. The question has been resolved

-
16 Sep 2011 7:10 AM #6
Please can you post the code ?



Reply With Quote