Topic
  • 1 reply
  • Latest Post - ‏2014-09-20T11:17:50Z by jitupkp
Jennifer90
Jennifer90
1 Post

Pinned topic Get Data from servlet to an ExtJs grid using ajax request

‏2013-06-16T10:26:29Z |

I'm newbie at ExtJs and i would to read data from a servlet and put this data into an ext js grid using ajax request, but i'm always having blank page! This is my ajax.html page:

<html>

<head>
<script type=
"text/javascript">
Ext.onReady(
function(){
    
var myData=
null;
    Ext.Ajax.request({
        url: 
'AccessServlet',
        method:
'POST',
        success: 
function ( result, request ) { 
        store.loadData(myData);
        myData =Ext.decode(result.responseText);
 
 
        },
        failure: 
function ( result, request) { 
                Ext.MessageBox.
alert(
'Failed', result.responseText); 
        } 
    });
     
var store = 
new Ext.data.ArrayStore({
            fields: [
               {
name: 
'name'},
               {
name: 
'profession'},
 
            ]
        });
 
     
//store.loadData(myData);
 
        
// create the Grid
        
var grid = 
new Ext.grid.GridPanel({
            store: store,
            columns: [
                {
 
                 header   : 
'Name', 
                    width    : 
160
 
                },
                {
                    header   : 
'Profession', 
                    width    : 
75 
 
                },
 
 
            ],
 
            height: 
350
 
        });
 
        grid.render(
'db-grid');
 
});
</script>
</head>
<body>
<h2>Ajax Demo</h2>
 
</body>
 
</html>

and this is My Servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    
// TODO Auto-generated method stub
 

//  PrintWriter out = response.getWriter();

//  out.println("This Example demonstrates, using Ajax request to Access Servlet ");
     response.setContentType(
"text/html");    
// Response mime type
 
     
// Output stream to STDOUT
     PrintWriter out = response.getWriter();
 
 
    JSONObject myObject = 
new JSONObject();
 
 
 
    myObject.put(
"name",
"Jennifer");
    myObject.put(
"profession",
"teacher");
 
out.println(myObject);
 
 
JSONObject myRecord = 
new JSONObject();
myRecord.put(
"name",
"Jacques");
myRecord.put(
"profession",
"Doctor");
 
 
JSONArray myRecords = 
new JSONArray();
myRecords.add(myRecord);
}

Help me please!!