1. #1
    Sencha User
    Join Date
    Jun 2013
    Posts
    7
    Vote Rating
    0
    Jennifer90 is on a distinguished road

      0  

    Default Unanswered: decode an invalid JSON String

    Unanswered: decode an invalid JSON String


    How can I retrieve json array returned from my servlet in my ajax request in order to put it into an ext js grid? Note: My application always returns an empty grid! This is my ajax request:
    Code:
    Ext.Ajax.request({
       url: 'src/AccessServlet.java',    method:'POST',    success: function ( result, request ) {    Ext.MessageBox.alert('Success', 'Data return from the server: '+ result.responseText);    myData =Ext.util.JSON.decode(result.responseText);    console.log(myData);    store.loadData(myData);    },    failure: function ( result, request) {          Ext.MessageBox.alert('Failed', result.responseText);     } }); var store = new Ext.data.ArrayStore({    fields: [{name: 'name'},       {name: 'id'},]});var grid = new Ext.grid.GridPanel({    store: store,    columns: [ {header   : 'Name',             width    : 60},        { header   : 'id',             width    : 60},],         height: 200});        grid.render('grid'); });
    and this is my servlet:
    Code:
         protectedvoid doGet(HttpServletRequest request,HttpServletResponse     response)throwsServletException,IOException
          {       response.setContentType("application/json");     System.out.println("Right!!You are in servlet now !!!!! ");    PrintWriter out = response.getWriter();    // Output stream to STDOUT    JSONObject myObject = new JSONObject();    myObject.put("name","xx");    myObject.put("id","123");    System.out.println(myObject);   JSONObject myRecord = new JSONObject();    myRecord.put("name","yy");    myRecord.put("id","12");    System.out.println(myRecord);    JSONArray myRecords = new JSONArray();    myRecords.add(myObject);    myRecords.add(myRecord);    System.out.println(myRecords); }
    Firebug shows: Ext.Error: You're trying to decode an invalid JSON String
    Could you help me please!

  2. #2
    Sencha - Community Support Team friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Vote Rating
    27
    Answers
    106
    friend has a spectacular aura about friend has a spectacular aura about

      0  

    Default


    The most common way to populate a grid is by using a Store. See the Ext.data.Store documentation for a good example, which uses Ajax and a JSON reader in the store's proxy.

    Also take a look at the Ext.grid.Panel documentation which has a great example of what your JSON should look like and how the grid/store wiring is done.

    Your Servlet URL looks jacked up also; it appears that you're trying to directly invoke a Java file, where you should drop the '.java' off the end of the URL. Also, Servlets are registered with your application context so it's not valid to prefix the Servlet URL with 'src/'.

    Once you get your Servlet working properly, you'll need to verify that it's returning data in a properly structured JSON format. You can test the output by calling your Servlet directly, in your browser's address bar (http://localhost:8080/yourAppContext/AccessServlet, if running on Tomcat locally). Copy the results from your Servlet, then go to http://www.jsonlint.com, paste the results and click the Validate button. This will tell you if your JSON is properly formatted.

    On a related note, I would avoid Servlets like the plague. There are a wealth of good web/MVC frameworks out there to help you. In particular, I strongly recommend taking a look at the Spring Web MVC Framework. Spring has a native 'view resolver' which will automatically serialize any client response to JSON, even when returning a custom Object, List, Map, etc.

Thread Participants: 1

Tags for this Thread