1. #1
    Ext User justmonkey's Avatar
    Join Date
    Mar 2007
    Location
    Beijing,China
    Posts
    26
    Vote Rating
    0
    justmonkey is on a distinguished road

      0  

    Question 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*/
    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;
        }
    }
    /*results.js*/
    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();
    });
    Anyone can help me? Thanks.

  2. #2
    Ext Premium Member
    Join Date
    Mar 2007
    Location
    Norway
    Posts
    196
    Vote Rating
    3
    bone is on a distinguished road

      0  
    Founder of the Path of Exticism

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    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.

  4. #4
    Ext User
    Join Date
    Mar 2007
    Posts
    31
    Vote Rating
    0
    tab is on a distinguished road

      0  

    Default


    Here is a Java example of generating some JSON
    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);
    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.

    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.

  5. #5
    Ext User justmonkey's Avatar
    Join Date
    Mar 2007
    Location
    Beijing,China
    Posts
    26
    Vote Rating
    0
    justmonkey is on a distinguished road

      0  

    Default


    Thank you all. The question has been resolved

  6. #6
    Sencha User
    Join Date
    Sep 2011
    Location
    Italy
    Posts
    80
    Vote Rating
    0
    spillo491 is on a distinguished road

      0  

    Default


    Please can you post the code ?