【问题标题】:Sencha Touch Cant decode and load JSON response to data modelSencha Touch 无法解码和加载 JSON 响应到数据模型
【发布时间】:2011-10-04 20:51:58
【问题描述】:

我对 sencha touch 获取服务器响应的问题之一感到非常痛苦。我有一个应用程序,我需要与我的服务器通信,而服务器又将与数据库交互并以 JSON 的形式提供信息。 现在,我的问题陈述是成功登录到应用程序需要加载模型的应用程序。模型使用代理下载 JSON 并在屏幕 UI 上呈现。问题从这里开始。

我正在使用 Servlet 来获取 JSON 服务。例如,我对 json 进行了硬编码。找到下面的 Servlet 代码。

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginJSONServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;       

    public LoginJSONServlet() {
        super();       
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
        try {
            response.setContentType("application/json");                        
            String jsonText = "[{\"quoteName\":\"Home care\",\"timeStamp\":\"May2011\"}]";                  
            response.getWriter().write(jsonText);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

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

部署后在浏览器中将所需的 JSON 打印为 {"quoteName":"Home care","timeStamp":"May2011"}

现在,这是我的客户端煎茶触摸代码

这是我的商店

App.stores.SavedStore = new Ext.data.JsonStore({
    model: 'SavedModel',
    autoLoad: true
});

这是我的模型

App.models.SavedQuoteMod = Ext.regModel('SavedQuoteMod', {
   fields: [
       {name:'quoteName' , type: 'string' },
       {name:'timeStamp' , type: 'string' }
   ],


   proxy:{
          type: 'ajax',
          format: 'sencha',
          url: 'http://localhost:8091/Sencha/LoginServlet',
          reader: {
               type: 'json',
               root: 'data'
          },


  /* proxy:{
    type: 'ajax',
    url: 'http://172.16.30.18:8091/Sencha/JSONServlet',
    reader: {type:'json'},
    writer: {type:'json'},
   }*/                       
});

我很困惑我必须使用 Ajax、JSONP 或其他任何代理请求类型。 我的代码不起作用。请帮我。

【问题讨论】:

    标签: sencha-touch extjs


    【解决方案1】:

    问题出在读者身上。如果您指定根 : 'data' ,它将需要一个看起来像这样的 json:data:[{"quoteName":"Home care","timeStamp":"May2011"},... 所以只需删除 root:'data' 行,它应该可以工作

    编辑商店是应该定义代理的人。

    App.stores.SavedStore = new Ext.data.JsonStore({
        model: 'SavedQuoteMod',
        autoLoad: true,
        proxy:{
              type: 'ajax',
              format: 'sencha',
              url: 'http://172.16.30.18:8091/Sencha/subhash.json',
              reader: {
                   type: 'json',
              },
    
    });
    
    App.models.SavedQuoteMod = Ext.regModel('SavedQuoteMod', {
       fields: [
           {name:'quoteName' , type: 'string' },
           {name:'timeStamp' , type: 'string' }
       ]
    });
    

    这样配置的商店需要一个包含元组数组的 json,例如 [{"quoteName":"Home care","timeStamp":"May2011"},这实际上是您发送的内容,如果您设置配置根对于代理,它希望商店的数据位于数据之类的属性中:[{....

    【讨论】:

    • @nscrob...我无法正确理解。你能解释一下吗?
    • @nscrob.. 现在浏览器抛出 Uncaught Error: You are using a ServerProxy but has not provided with a url.
    • 我相信你使用的是 extjs 4,所以商店应该是 Ext.data.Store,而不是 JsonStore
    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2013-08-21
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多