PDA

View Full Version : decode an invalid JSON String



Jennifer90
18 Jun 2013, 3:13 AM
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:

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:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, 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!

friend
18 Jun 2013, 4:15 AM
The most common way to populate a grid is by using a Store. See the Ext.data.Store (http://docs.sencha.com/extjs/4.2.1/#!/api/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 (http://docs.sencha.com/extjs/4.2.1/#!/api/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 (http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html). Spring has a native 'view resolver' which will automatically serialize any client response to JSON, even when returning a custom Object, List, Map, etc.