Results 1 to 2 of 2

Thread: decode an invalid JSON String

  1. #1
    Sencha User
    Join Date
    Jun 2013
    Vote Rating

    Default 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:
       url: 'src/',    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{    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:
         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 User friend's Avatar
    Join Date
    Apr 2011
    Vote Rating


    The most common way to populate a grid is by using a Store. See the 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, 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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts