如何在我的 ajax 请求中检索从我的 servlet 返回的 json 数组,以便将其放入 ext js 网格中?注意:我的应用程序总是返回一个空网格!这是我的ajax请求:
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');
});
这是我的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);
}
我的 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ajaxWithServlet</display-name>
<welcome-file-list>
<welcome-file>ajax.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>AccessServlet</display-name>
<servlet-name>AccessServlet</servlet-name>
<servlet-class>AccessServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AccessServlet</servlet-name>
<url-pattern>/AccessServlet</url-pattern>
</servlet-mapping>
</web-app>
请帮帮我!