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

      0  

    Default Ext Js Grid

    Ext Js Grid


    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:'url',     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 =newExt.data.ArrayStore({     fields:[{name:'name'},{name:'id'},]});var grid =newExt.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 !!!!! ");PrintWriterout= response.getWriter();// Output stream to STDOUTJSONObject myObject =newJSONObject();     myObject.put("name","xx");     myObject.put("id","123");System.out.println(myObject);JSONObject myRecord =newJSONObject();     myRecord.put("name","yy");     myRecord.put("id","12");System.out.println(myRecord);JSONArray myRecords =newJSONArray();     myRecords.add(myObject);     myRecords.add(myRecord);System.out.println(myRecords);}

    Note:
    I get the response in JSON format only if I run my servlet but if I run my application I get no response from my servlet!
    Help me please !

  2. #2
    Sencha User existdissolve's Avatar
    Join Date
    Jan 2010
    Location
    Kansas
    Posts
    522
    Vote Rating
    86
    existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice

      0  

    Default


    Are you getting errors when making the AJAX request?

    Does you AJAX request have proper access to the servlet's URL (e.g., not cross-domain request)?

    Without more information, it's tough to know what the issue might be.

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

      0  

    Default


    I receive this error when using Firebug
    Ext.Error: You're trying to decode an invalid JSON String
    and when executing the servlet only i got that response
    Right!!You are in servlet now !!!!!
    {"name":"xx","id":"123"}
    {"name":"yy","id":"12"}
    [{"name":"xx","id":"123"},{"name":"yy","id":"12"}]

  4. #4
    Sencha User existdissolve's Avatar
    Join Date
    Jan 2010
    Location
    Kansas
    Posts
    522
    Vote Rating
    86
    existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice existdissolve is just really nice

      0  

    Default


    The Ext.Error would seem to indicate that something other than valid JSON is being returned and that the proxy is choking on parsing the response. Can you post the actual response from the server that shows up in Firebug when you click on the URL that was requested?

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

      0  

    Default


    d'accord
    voila l'erreur que m'indique Firebug
    Ext.Error: You're trying to decode an invalid JSON String: import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;


    /**
    * Servlet implementation class AccessServlet
    */
    public class AccessServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public AccessServlet() {
    super();
    // TODO Auto-generated constructor stub
    }
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //response.setContentType("application/json");
    response.setContentType("text/html");
    //System.out.println("You are in servlet now !!!!! ");
    // Output stream to STDOUT
    /* PrintWriter out = response.getWriter();
    JSONObject myObject = new JSONObject();
    myObject.put("name","xx");
    myObject.put("id","123");

    //System.out.println("JSON data: "+myObject.toString());
    out.println(myObject);


    /* JSONObject myRecord = new JSONObject();
    myObject.put("name","yy");
    myObject.put("id","12");

    JSONArray myRecords = new JSONArray();
    myRecords.add(myRecord);
    */

    }
    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    response.setContentType("application/json");
    //response.setContentType("html/text");
    // TODO Auto-generated method stub

    //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("JSON data: "+myObject.toString());
    out.println(myObject);


    JSONObject myRecord = new JSONObject();
    myRecord.put("name","yy");
    myrecord.put("id","12");

    JSONArray myRecords = new JSONArray();
    myRecords.add(myObject);
    myRecords.add(myRecord);
    out.println(myRecords);
    }
    }

  6. #6
    Sencha User filippo.ferrari's Avatar
    Join Date
    Apr 2010
    Location
    Sanremo - Italy
    Posts
    126
    Vote Rating
    1
    filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute

      0  

    Default


    Could you post only the Json?

    If Firebug receive a valid Json shows a tab named "JSON", Can you see it?

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

      0  

    Default


    No i don't see it Now i have another problem, i receive an other error from Firebug which is
    TypeError: me.fields is undefined
    [IMG]chrome://firebug/content/blank.gif[/IMG] fields = me.fields.items;
    ext-all-debug.js (ligne 70616)


    What does this mean?
    I didn't find any solution for that!

  8. #8
    Sencha User filippo.ferrari's Avatar
    Join Date
    Apr 2010
    Location
    Sanremo - Italy
    Posts
    126
    Vote Rating
    1
    filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute filippo.ferrari has a reputation beyond repute

      0  

    Default


    I think it depends to the incorrect json response.

    For debugging I usually link a file in my ajax request that respond a plain text that is a well formatted json.
    Then the goal is provide a dinamic response that is still a well formatted json.

    F.

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

      0  

    Default


    This is the json record that i receive when i execute my servlet on Eclipse
    [{"name":"xx","id":"123"}]
    But by executing it on Mozilla Firefox,Firebug show me the same error that i have put above
    TypeError: me.fields is undefined
    [IMG]chrome://firebug/content/blank.gif[/IMG] fields = me.fields.items;
    Have you any idea how can i resolve the problem because I'm new with those notions?