【问题标题】:json array into Ext js Gridjson 数组到 Ext js Grid
【发布时间】:2013-06-13 12:13:54
【问题描述】:

如何在我的 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>

请帮帮我!

【问题讨论】:

  • 你定义了网格吗? store 是为该网格配置的吗?什么不适合你?因为基本上你在这里所做的应该可以工作。
  • 是的,我已经定义了我的网格,并且 store 是为我的网格配置的!除了我的 servlet 成功返回一个 json 数组!但是网格总是空的!!
  • 我该怎么办?!我需要你的帮助
  • 你也可以发布 JSON 吗?
  • 好的,我在上面发布了我的 servlet 代码!

标签: ajax json parsing servlets extjs


【解决方案1】:

我认为您的问题是您的 JsonDataStore 的 URL。您不能通过目录结构访问您的 servlet。简直行不通。将 servlet 的应用程序服务器 URL 指定为 DataStore 中的 url 属性。您应该会发现这很有用。

Can't get jSon dataStore into ExtJS (Sencha Touch) chart: displays error "cannot read property 'length' of undefined"

好的。在您的 ajax 请求中,您已将 url 指定为“url:src/AccessServlet.java”。这是您尝试直接访问源文件。它不起作用,因为除非编译,否则任何 Java 源文件都无法运行。 Servlet 是一样的。此外,您必须在 web.xml 文件中映射您的 servlet。部署 Web 应用程序时,servlet 将通过映射在 web.xml 中的 url 可用。在 ajax 请求中使用此 url。另一件事是,在您的 servlet 中,您必须将 json 对象写入响应。为此使用 PrintWriter 实例。

http://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html

【讨论】:

  • 对不起,我没明白你的意思。你能解释一下吗?!
  • 我从一开始就将我的 servlet 映射到我的 web.xml 中,并且我使用 printWriter 将我的 json 对象写入响应,但仍然存在同样的问题。
  • 请同时发布您的 web.xml
  • 好的,我将通过添加我的 webxml 文件来编辑我的帖子!!注意:通过使用 Firebug,它会显示此错误:Ext.Error: You're trying to decode an invalid JSON String
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多